views:

239

answers:

2

I'm trying to programatically determine the cluster size of a storage card, using C# / .NET Compact Framework, on Widows Mobile. For desktop Windows there's the GetDiskFreeSpace() function, but it doesn't exist in coredll.dll/Windows Mobile.

Is there any other way I could find out the size of the cluster for a storage card?

+1  A: 

This forum post gives some ideas of someone else having this problem.

JasonRShaver
+1  A: 

I haven't tried this myself, but you can try the CeGetVolumeInfo and check the dwBlockSize value. This looks like it could be the cluster size.

If that doesn't work then it gets a little more involved.

Storage cards are normally formatted in the FAT format.

You need to access the low level routines in CE to read at a disk level and read the FAT BPB it determine what type of FAT and what the cluster size is.

Use Storage Manager functions FindFirstStore / FindNextStore to find the storage card you are after. Then open the storage card using the CreateFile API.

HANDLE hDisk(CreateFile(storeInfo.szDeviceName, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_WRITE|FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL));

Then you need to read the FAT BPB which is on the first sector for super disks (which would be the normal case for Storage Card formatted devices) or it's in MBR format.

  SG_REQ req;
  DWORD cb;

  req.sr_start = 0;
  req.sr_num_sec = 1;
  req.sr_num_sg = 1;
  req.sr_status = 0;
  req.sr_callback = 0;
  req.sr_sglist[0].sb_buf = sectorBuffer;
  req.sr_sglist[0].sb_len = storeInfo.dwBytesPerSector;

  DeviceIoControl(hDisk, DISK_IOCTL_READ, &req, sizeof(req), 0, 0, &cb, 0);

Once you have the BPB you need to determine what fat format it is (FAT12/FAT16/FAT32) and then pull out the cluster size from it.

How you do the above in C# is up to you. I see in the storage manager reference it can go down to the partition level and you can query the partition type. That will tell you the FAT type so you don't need to figure it out.

Shane Powell