July 29 2010




 
Search Blog Entries:



What is this?

Code Details
 
Available memory in PocketPC and CF Flash

Description: 

The available memory of a PocketPC can be found, calling the API GetDiskFreeEx. The first parameter to this API is a path to a particular directory. For the device itself, this path is the root directory. If a storage card is inserted this path should be: [\storage card]. In the following code snippet we show how to create a little class with a static member function to obtain availabe storage memory, making use of P/Invoke in combination with the GetDiskFreeEx API.



Code:

public class MemStatus
{
  [DllImport("coredll.dll")]
  public static extern bool GetDiskFreeSpaceEx (
    string lpDirectoryName,
    out ulong lpFreeBytesAvailableToCaller,
    out ulong lpTotalNumberOfBytes,
    out ulong lpTotalNumberOfFreeBytes);

  public const string STORAGE_INTERNAL = "\\";
  public const string STORAGE_FLASH_CARD =
    "\\storage card\\";

  public MemStatus()
  {
  }

  public static bool GetStorageInfo (
      string storagePath,
      out ulong totalBytes,
      out ulong availBytes)
  {
    ulong freeBytesAvail;
    ulong totalBytesAvail;
    ulong freeBytesTotal;
    bool result = GetDiskFreeSpaceEx (storagePath,
       out freeBytesAvail,
       out totalBytesAvail,
       out freeBytesTotal);

    if (result == true)
    {
      totalBytes = ulong.totalBytesAvail;
      availBytes = ulong.freeBytesAvail;
    }
    else
    {
      totalBytes = ulong.MaxValue;
      availBytes = ulong.MaxValue;
    }

    return result
  }
}


// Call this sample code like this:

ulong totalBytes;
ulong availBytes;

if (MemStatus.GetStorageInfo(
    MemStatus.STORAGE_FLASH_CARD,
    out totalBytes,
    out availBytes))
{
  label1.Text = totalBytes.ToString();

  label2.Text = availBytes.ToString();

else 
{
  label1.Text = "?";
  label2.Text = "?";
}







Send us your solutions, code, or advice. We might put it here on the site!
 
Back








SpiralFX Web Development
www.spiralfx.com


Do you want to learn developing a full blown Windows Mobile Application? This article and accompanying multimedia content will help you to do so. It will be extended over the upcoming weeks / months, so check back regularly.
 
Read Full Article