I am currently working on a SaveAs feature to some special file formats, those files are very very big. Before actually saving the file, I need to compute its anticipated size and compare it with the free disk space to see if there is enough storage for that operation. After searching a bit about how to get the free disk space, I came across the solution which uses the System.Management namespace. This namespace provides access to a rich set of management information about the system, devices, and applications instrumented to the WMI infrastructure. But, what the hell is WMI?


WMI is Windows Management Instrumentation and is part of the Windows operating system that provides management information and control. WMI provides extensive instrumentation to accomplish almost any management task and help us obtain information about our system. Applications and services can query for interesting management information such as how much free space is left on the disk, what is the current CPU utilization, which database a certain application is connected to, and much more. Here is the MSDN page about the Windows Management Instrumentation.

Let me show you how to query the free disk space using WMI:

private static ulong GetFreeDiskSpaceInBytes(string drive)
    ManagementObject disk = 
        new ManagementObject("win32_logicaldisk.deviceid=\"" + drive + ":\"");
    return (ulong)disk["FreeSpace"];

Notice that I use the ManagementObject class which have a very poor documentation in MSDN: “Represents a WMI instance“. The constructor initializes a new instance of that class for the specified WMI object path which is provided as a string. I agree that this is a very bad API and an Enum would have been much better, but we have to live with that. The same logic is implemented for querying a specific property from the object, we shall provide the property string and convert the returned object to a specific type, unboxing it.

If you need to know the available WMI paths for the ManagementObject constructor, you start digging here. Browse to a specific class to see the list of all available properties, here is the property list for the Win32_LogicalDisk Class. Moreover, you can download the WMI Administrative Tools to view WMI classes, properties, qualifiers, and instances. After downloading it, click on the “WMI Object Browser” under the “WMI Tools” in the Programs group:


Upon running the object browser you will be presented a screen as follows:


Click OK. You may get a “WMI Object Browser Login” popup, Click Ok and you will be presented with the following screen:


We can now browse for a specific provider class by clicking on the search button on the upper left side of the screen (near the “Objects In” section). Here is how it looks:


After choosing the selected object for browsing, you get all of its properties, methods and associations:


Lets take a look at another code using WMI to the get Windows last Boot uptime:

private static DateTime GetWindowsUptime()
    ManagementClass operSystem = new ManagementClass("Win32_OperatingSystem");
    ManagementObjectCollection mngInstance = operSystem.GetInstances();
    foreach (ManagementObject mngObject in mngInstance)
        string lastBootObj = mngObject["LastBootUpTime"].ToString();
        DateTime lastBootUp = ManagementDateTimeConverter.ToDateTime(lastBootObj);
        if (lastBootUp != DateTime.MinValue)
            return lastBootUp;
    return DateTime.MinValue;

We are querying the operating system class for its LastBootUpTime property. The WMI is very powerfull. WMI is designed for programmers who use C/C++, C#, Visual Basic .NET and ASP.NET. Notice that WMI is included with Windows 2000, Windows XP, Windows Vista or Windows ME edition. While with Windows 95, Windows 98, and Windows NT 4.0 you must download a separate WMI installation package from MSDN. If you have any question, thought or clarification leave your commet.

Tags :

6 Responses to “How To Get Free Disk Space And Other WMI Stuff (.NET)”

  1. Karl Agius

    Said on October 30, 2008 :

    I agree that WMI is extremely powerful, and that it lets you get to details that are not otherwise available in managed code. In this case though, the free drive space can be derived through the AvailableFreeSpace method in DriveInfo. What are the arguments for using WMI instead of this?

    Great article, by the way :D That WMI object browser looks handy … sure beats trawling through MSDN looking for stuff ;) Thanks!

  2. Matt

    Said on November 1, 2008 :

    Great article. If you don’t want to use a WMI object browser that looks like it was written in Visual Basic 3.0, I’d highly recommend checking out Terminals:

    Terminals is a tabbed remote desktop client, but it also has a number of useful tools built in, one of which is a WMI browser.

  3. Brian Schmitt

    Said on November 4, 2008 :

    I would also recommend the tool Scriptomatic
    It will generate WMI scripts for you, which you can then use to figure out/test with.

  4. tashuco tumico

    Said on May 22, 2009 :

    How I can make it on a remote server?

    W2k3 is my case

  5. Vineet

    Said on June 24, 2009 :


    Does anyone know how to get information about the database to which an application is connected to using WMI classes?


  6. Vineet

    Said on June 24, 2009 :


    Does anyone know how to get information about the database to which an application is connected to using WMI classes? I am using Java :)


Post a Comment