I need to determine how much free space there is on a Windows CE device, to conditionally determine whether a particular operation should proceed.
I thought Ken Blanco's answer here (which bears a striking similarity to the example yonder) would work, which I adapted as:
internal static bool EnoughStorageSpace(long spaceNeeded)
{
DriveInfo[] allDrives = DriveInfo.GetDrives();
long freeSpace = 0;
foreach (DriveInfo di in allDrives)
{
if (di.IsReady)
{
freeSpace = di.AvailableFreeSpace;
}
}
return freeSpace >= spaceNeeded;
}
...but DriveInfo is not available in my Windows CE / compact framework project.
I am referencing mscorlib, and am using System.IO, but as DriveInfo is redder than a Kansas City Chiefs jersey in my editor, I reckon it's not available to me.
Is there an alternative way of accomplishing the same thing?
UPDATE
I adapted this:
[DllImport("coredll.dll", SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetDiskFreeSpaceEx(string lpDirectoryName,
out ulong lpFreeBytesAvailable,
out ulong lpTotalNumberOfBytes,
out ulong lpTotalNumberOfFreeBytes);
public static bool EnoughStorageSpace(ulong freespaceNeeded)
{
String folderName = "C:\\";
ulong freespace = 0;
if (string.IsNullOrEmpty(folderName))
{
throw new ArgumentNullException("folderName");
}
ulong free, dummy1, dummy2;
if (GetDiskFreeSpaceEx(folderName, out free, out dummy1, out dummy2))
{
freespace = free;
}
return freespace >= freespaceNeeded;
}
...from here, which compiles, but I don't know what "folderName" should be for a Windows CE device; in Windows Explorer, it has no name at all. I'm sure what I have as of now ("C:\") is not right...
UPDATE 2
According to "Windows programmer" here: "If you're running Windows CE then \ is the root directory"
So, should I use:
String folderName = "\";
...or do I need to escape it:
String folderName = "\\";
...or...???