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
LVL 3
sasllcAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

planoczCommented:
You will have to add refence of System.Management
Then try this  sample code.....
Imports System.Management

   Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Label1.Text = Format(GetDiskSpace(), "###,####,###,###") & " bytes of free space"
    End Sub

    Public Function GetDiskSpace() As System.UInt64
        Dim diskClass As _
            New System.Management.ManagementClass("Win32_LogicalDisk")
        Dim disks As System.Management.ManagementObjectCollection = _
            diskClass.GetInstances()
        Dim disk As System.Management.ManagementObject
        Dim space As System.UInt64
        For Each disk In disks
            If CStr(disk("Name")) = "C:" Then
                space = CType(disk("FreeSpace"), System.UInt64)
            End If
        Next disk
        Return space
    End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sasllcAuthor Commented:
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?

0
planoczCommented:
You may need to look an see if it is in the compact framework.
Why do you need to know the free space?
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

pgnatyukCommented:
You can call GetDiskFreeSpaceEx. Here is an example:

CodeProject. Display device memory information using P/Invoke
http://www.codeproject.com/KB/mobile/DeviceMemoryInfo.aspx
0
sasllcAuthor Commented:
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?

0
pgnatyukCommented:
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

0
pgnatyukCommented:
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

0
planoczCommented:
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.
0
sasllcAuthor Commented:
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?


0
pgnatyukCommented:
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

0
sasllcAuthor Commented:
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

0
pgnatyukCommented:
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
0
sasllcAuthor Commented:
I would request that this question NOT be closed out as abandoned.  I am still working with these recommendations as quickly as I can.
0
planoczCommented:
asallc  are you still having problems with this report?
0
sasllcAuthor Commented:
Yes, but if I cannot get it resolved within the next couple of weeks, I will close out this question.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows OS

From novice to tech pro — start learning today.