Link to home
Start Free TrialLog in
Avatar of sasllc
sasllcFlag for United States of America

asked on

How to get available disk space in compact framework environment?

Our apps run on Windows CE 5.0 devices, written in vb.net 2005, in the compact framework 2.0 environment.  If I go to a command prompt on the device and do a 'DIR', I can get exactly what I need by viewing the number of bytes free.  

How can I get this same number--bytes free--programmatically?  TIA
ASKER CERTIFIED SOLUTION
Avatar of Howard Cantrell
Howard Cantrell
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sasllc

ASKER

Unfortunately it isn't letting me add "Imports system.management"--presumably because I'm working with portable devices??  "Imports system." gives me quite a few options, but system.management is not listed.  What to do?

You may need to look an see if it is in the compact framework.
Why do you need to know the free space?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sasllc

ASKER

I need to know in order to programmatically protect my customer from a recurrence of a recent event where they ran out of space during data collection, ultimately resulting in many lost man-hours.

As mentioned in my opening line above "Our apps run on Windows CE 5.0 devices, written in vb.net 2005, in the compact framework 2.0 environment. ", this app does indeed run in the compact framework environment, and is in vb.net.  Is there a vb.net version of the code shown at the codeproject website?

I posted the link where you can see how to use P/Invoke. The code is in C#.

Here is the same in VB.NET
http://www.freevbcode.com/ShowCode.asp?ID=5588

This is MSDN: http://msdn.microsoft.com/en-US/library/aa912270.aspx
GetDiskFreeSpaceEx exists from Windows CE 2.0.

System.IO.DriveInfo does not exist in Compact Framework, so you need to use GetDiskFreeSpaceEx from coredll.

In case I didn't understand you and you need the device memory:
http://msdn.microsoft.com/en-us/library/ms172518.aspx

I think pqnatyuk has the right idea. Microsoft has really cut down on the size of the compact framework to help in memory storage. GetDisFreeSpaceEx should do the job for you.
Avatar of sasllc

ASKER

pgnatyuk, I need help on how to use this GetDiskSpaceEx function(?).  I simply keyed this in to my program:

Call GetDiskFreeSpaceEX("\", piWork1, piWork2, piWork3)

and I got no syntax errors, and it prompted me for the variables as I keyed it in.

But when I run the program, I get a MissingMethodException, teling me that it can't find pInvoke DLL 'kernel32'.  What does that mean/how do I fix it?

Note that I did not key in any of that code list in the MSDN link.  Do I need to do that as well?  If so, is it available in vb.net?


Here: http://www.freevbcode.com/ShowCode.asp?ID=5588 you can find the VB code.
Copy the declarations and code below and paste directly into your VB project.
Private Declare Function GetDiskFreeSpaceEx _
    Lib "coredll" _
    Alias "GetDiskFreeSpaceExA" _
    (ByVal lpDirectoryName As String, _
    ByRef lpFreeBytesAvailableToCaller As Long, _
    ByRef lpTotalNumberOfBytes As Long, _
    ByRef lpTotalNumberOfFreeBytes As Long) As Long

I attached the rest of the code.

You need to check this name - coredll. kernel32 is for a pc version of Windows.

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 Long

        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 Long

        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

Open in new window

Avatar of sasllc

ASKER

When I try to run this, I get an error very similar to what I listed before: "Can't find an entry point 'GetDiskFreeSpaceExA' in a PInvoke DLL 'coredll'."

When you say "You need to check this name - coredll. kernel32 is for a pc version of Windows", what exactly do you mean?

FWIW, I DO have a reference to coredll elsewhere in in a different module in my program that is used to play sounds on the scanner's speakers--see code below.  This was another case where I copied some code years ago, and don't really understand what it's doing, or where coredll.dll is coming from, etc.

So....what should I change to prevent this runtime error?


    Public Class win32
        Public Declare Function PlaySoundW Lib "coredll.dll" Alias "PlaySoundW" (ByVal lpszName As String, ByVal hModule As Integer, ByVal dwFlags As Integer) As Integer
        Public Const SND_FILENAME = &H20000
        Public Const SND_ASYNC = &H1
        Public Const SND_SYNC = &H0
    End Class

Open in new window

You need to use coredll.dl and GetDiskFreeSpaceExW. Same way as you have it for PlaySound.

Private Declare Function GetDiskFreeSpaceEx Lib "coredll.dll" Alias "GetDiskFreeSpaceExW" _
    (ByVal lpDirectoryName As String, _
    ByRef lpFreeBytesAvailableToCaller As Long, _
    ByRef lpTotalNumberOfBytes As Long, _
    ByRef lpTotalNumberOfFreeBytes As Long) As Long
Avatar of sasllc

ASKER

I would request that this question NOT be closed out as abandoned.  I am still working with these recommendations as quickly as I can.
asallc  are you still having problems with this report?
Avatar of sasllc

ASKER

Yes, but if I cannot get it resolved within the next couple of weeks, I will close out this question.