I am attempting to determine the amount of free space a CD has using the following code:
ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_LogicalDisk WHERE DriveType =5");
foreach(ManagementObject mo in searcher.Get())
{
Console.WriteLine( "FreeSpace: "+mo["FreeSpace"].ToString());
Console.WriteLine("CapacitySpace: " + mo["Size"].ToString());
UInt64 usedspace = (UInt64)mo["Size"] - (UInt64)mo["FreeSpace"];
Console.WriteLine("UsedSpace: " + usedspace.ToString());
}
Running the above code, I receive the following output:
FreeSpace: 0
CapacitySpace: 301463552
UsedSpace: 301463552
Ideally, I’d like to report the Windows Explorer statistics – XXX of YYY free. Please note that Windows Explorer is reporting 392 MB free of 702 MB. Thanks!
Update (1 Apr 09):
It appears the capability of determining a CD’s free space is beyond the WMI and is reliant on the file system of the disk inserted. My testing indicates that Windows will not display capacity information for CDs formatted CDFS; however, it will display capacity information for CDs formatted UDF.
Also, I located an excellent native utility for browsing the WMI referenced here.
Are you attempting to determine the amount of space unused from the theoretical max available of a CD?
My understanding is that a CD-ROM is a read-only format, and thus will never have any free space. Every bit is used for data already written, no more can be written to it, so there will never be space available.
If you just want to know the unwritten space on a ROM, simply subtract the used space from the fixed theoretical max (702MB?), and there you have it. The fixed max should be part of a spec somewhere.
Have you looked into using mciSendString() on the device, possibly with the “info” or “status” strings, to get further information on the disc in the drive? I haven’t tried this myself, so I’m not sure it will work.