Nov 7 2008

Detecting Storage Cards

Category: Windows CE | Programming | WindowsJoel Ivory Johnson @ 17:37

Some time ago in the MSDN forms I responded to a few questions on how to detect the storage cards in the file system and how does one get the storage card's capacity. I wrote the following could are a response to those questions and thought it would be helpful to share.  Storage cards show up in the file system as temporary directories.  This program examines the objects in the root of the device and any folders that have temp attribute are considered to be a positive match

using System;
using System.IO;
using System.Runtime.InteropServices;

namespace StorageCardInfo
{
  class Program
  {
    const ulong Megabyte = 1048576;
    const ulong Gigabyte = 1073741824;

 

    [DllImport("CoreDLL")]
    static extern int GetDiskFreeSpaceEx(
       string DirectoryName,
       out ulong lpFreeBytesAvailableToCaller,
       out ulong lpTotalNumberOfBytes,
       out ulong lpTotalNumberOfFreeBytes 
    );

    static void Main(string[] args)
    {
      DirectoryInfo root = new DirectoryInfo(@"\");
      DirectoryInfo[] directoryList = root.GetDirectories();
      ulong FreeBytesAvailable;
      ulong TotalCapacity;
      ulong TotalFreeBytes;

      for (int i = 0; i < directoryList.Length; ++i)
      {
        if ((directoryList[i].Attributes & FileAttributes.Temporary) != 0)
        {
          GetDiskFreeSpaceEx(directoryList[i].FullName, out FreeBytesAvailable, out TotalCapacity, out TotalFreeBytes);
          Console.Out.WriteLine("Storage card name: {0}", directoryList[i].FullName);
          Console.Out.WriteLine("Available Bytes : {0}", FreeBytesAvailable);
          Console.Out.WriteLine("Total Capacity : {0}", TotalCapacity);
          Console.Out.WriteLine("Total Free Bytes : {0}", TotalFreeBytes);
        }
      }
    }
  }
}

 

 

 

Tags: