tags:

views:

209

answers:

2

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.

A: 

Whats the object type of ManagementObject mo? Because if mo["Size"] only stores int32 then a cast to int64 wont do much.

PoweRoy
I'm the first to admit that I don't know a whole lot about the WMI / ManagementObject world but this wouldn't have an impact on the first console output (FreeSpace).
Joel
+1  A: 

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.

Furious Coder
That's what I'm attempting to do; however, the UsedSpace is reported as 301463552 Bytes (287.49MB) which would equate to 414MB (702-287=415) free. Explorer reports that I have 392MB free. In addition, your approach works well for CD but DVDs have different capacities (http://tinyurl.com/c3y5ln).
Joel
I haven't tried the mciSendString() yet but I took your earlier advice by using the theoretical maximum since I know what type of disc is inserted - a bit kludgey for my tastes but it does the trick in my limited testing.
Joel