Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting Free Disk Space

Status
Not open for further replies.

prgmrgirl

Programmer
Feb 12, 2004
119
US
Hey guys,

I'm trying to figure out the difference between these two ways of getting free disk space:

One uses a System.Management.ManagementObject:

Code:
ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
disk.Get();
return disk["FreeSpace"]

and the other uses DriveInfo:
Code:
DriveInfo drive = new DriveInfo("C");
return drive.AvailableFreeSpace();

Is there a reason you'd use one versus the other?

Thanks a bunch!
prgmrgirl
 
are they reporting the same figure? if so i would go with DriveInfo the code is much cleaner and more expressive.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Thanks for your response!

Yes they report the same figure. My understanding is that the ManagementObject uses WMI. But I don't know how DriveInfo is getting its data. I looked around but couldn't find anything definitive about it. I suspect it's using some kind of API call in the background.

DriveInfo would be my choice as well. Thing is, I'm looking at someone else's code and I'm not sure if he chose to use the ManagementObject for a reason that may not be readily apparent to me or if it was just a random choice. As far as I can see, the method is simply returning the free space.

Bascially, I'm not sure if there *is* a difference between the two.

BTW Jason: I've seen your replies to others and I just wanted to say that your responses have been some of the best and most knowledgable I've seen. Wherever it is you ply your trade, they are lucky to have you.

prgmrgirl

 
Info:
There is a wonderful function GetDiskFreeSpaceEx in Windows API. Three out parameters of the function: see C# DriveInfo members Avail... and Total... ;)
 
you can use Reflector to disassemble the code. and now that MS made the BCL source available you can see exactly what's happening.

If you don't already have them in place i would recommend source control and regression tests.
Source control to quickly and easily roll back changes if necessary.
Regression tests to provide feedback whether a code change breaks functionality.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Thanks a lot guys! This stuff if good to know.

Have a virtual beer on me! [thumbsup2]

prgmrgirl

 
LOL ArkM! Hint hint nudge nudge :)

I suspected as much. Thanks for confirming!
 
I try to stay aware from WMi as much as possible. I find it rather slow. Most of what WMI offers seems to have dot-net couterparts. At least in my needs.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top