Trying to find .NET CF 2.0 equivalent for GetDiskFreeSpace

G

Guest

Hello.

I'm running Win Mobile 5.0 on an ARM XScale, and writing in VS2005 - Visual
Basic with Compact Framework 2.0.

I want to find out (programmaticaly) how much storage space is available on
the Pocket PC, both in main storage ("\") and on a storage card ("\Storage
Card\"), and show a message box if either has less than 16 MB free.

I've tried the following inside a "Public Class Form1" but get a
NotSupportedException error:

Public Declare Function GetDiskFreeSpaceExW Lib "CoreDll.Dll" ( _
ByRef DirectoryName As String, _
ByRef FreeBytesAvailable As Int64, _
ByRef TotalNumberOfBytes As Int64, _
ByRef TotalNumberOfFreeBytes As Int64) As Boolean

Dim res As Boolean

res = Form1.GetDiskFreeSpaceExW("\", FreeBytesAvailable, TotalNumberOfBytes,
TotalNumberOfFreeBytes)

MsgBox(FreeBytesAvailable.ToString)

I've also tried using GlobalMemoryStatus and GetSystemMemoryDivision - these
work without raising an exception, but seem to give me program memory instead
of storage space.
 
G

Guest

Thanks for the suggestion, but dropping the W didn't make a difference - I
still get
a "NotSupportedException".

I've since been able to use GetStoreInformation() to acquire main storage
space, but still no success with '\Storage Card'.
 
G

Guest

Looks like I needed to pass the directory name by value instead of by
reference. Issue resolved.
 
G

Guest

A String is a reference type, so it's already being passed as a pointer.
Passing it ByRef sends a pointer to a pointer, which is the likely cause of
failure.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Managed Code in an Embedded World
www.OpenNETCF.com
 
A

amiga500

Hello,

I have same problem but I am unable to solve it. Here is my code:


--- Declaration
Private Declare Function GetDiskFreeSpaceEx Lib "coredll.dll" Alias
"GetDiskFreeSpaceEx" (ByVal lpDirectoryName As String, ByRef
lpFreeBytesAvailableToCaller As Long, ByRef lpTotalNumberOfBytes As
Long, ByRef lpTotalNumberOfFreeBytes As Long) As Long

--- Function


Public Function GetFreeSpace(ByVal Drive As String) As Long
'returns free space in MB, formatted to two decimal places
'e.g., msgbox("Free Space on C: "& GetFreeSpace("C:\") & "MB")

Dim lBytesTotal, lFreeBytes, lFreeBytesAvailable As Long

Dim iAns As Integer

iAns = GetDiskFreeSpaceEx(Drive, lFreeBytesAvailable, _
lBytesTotal, lFreeBytes)
If iAns > 0 Then

Return BytesToMegabytes(lFreeBytes)
Else
Throw New Exception("Invalid or unreadable drive")
End If
End Function

Public Function GetTotalSpace(ByVal Drive As String) As String
'returns total space in MB, formatted to two decimal places
'e.g., msgbox("Free Space on C: "& GetTotalSpace("C:\") &
"MB")

Dim lBytesTotal, lFreeBytes, lFreeBytesAvailable As Long

Dim iAns As Integer

iAns = GetDiskFreeSpaceEx(Drive, lFreeBytesAvailable, _
lBytesTotal, lFreeBytes)
If iAns > 0 Then

Return BytesToMegabytes(lBytesTotal)
Else
Throw New Exception("Invalid or unreadable drive")
End If
End Function

Private Function BytesToMegabytes(ByVal Bytes As Long) _
As Long


Dim dblAns As Double
dblAns = (Bytes / 1024) / 1024
BytesToMegabytes = Format(dblAns, "###,###,##0.00")

End Function


--- Use

'' SqlMobile.sdf located in Program Files\daedalusmobile
If GetFreeSpace("\Program Files\daedalusmobile\") <= 1 Then
If MessageBox.Show("You are low or out of space. Please
dock your equipment, complete some tasks, and then synchronize.",
MessageBoxButtons.OK) = DialogResult.OK Then
MyApp.fs.Stop()
End If
End If

When I run the program I get this error message:

NotSupportedException

Can someone please help me? Thanks in advance.
 
G

Guest

Public Declare Function GetDiskFreeSpaceExW Lib "CoreDll.Dll" ( _
ByVal DirectoryName As String, _
ByRef FreeBytesAvailable As Int64, _
ByRef TotalNumberOfBytes As Int64, _
ByRef TotalNumberOfFreeBytes As Int64) As Boolean

Dim res As Boolean

res = Form1.GetDiskFreeSpaceExW("\", FreeBytesAvailable, TotalNumberOfBytes,
TotalNumberOfFreeBytes)

MsgBox(FreeBytesAvailable.ToString)
 
G

Guest

It returns a BOOL which is 32-bits (Integer) not 64 (Long). You have your
declaration wrong.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Managed Code in an Embedded World
www.OpenNETCF.com
 
Top