After publishing the How To Get Free Disk Space And Other WMI Stuff (.NET) article, I learned two new things. One is about a better way to get free disk space and the other is about a better and easier way to use WMI in general. This is what is good about blogging, you share your knowledge and then learn from others.


Better Method For Getting Free Disk Space

Karl Agius left the following comment:

“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!”

Well, it turns out that instead of the method I wrote, I could just use:

DriveInfo drive = new DriveInfo(driveName);
long freeSpace = drive.AvailableFreeSpace;

Nice! I was sure that it is implemented with WMI, so I browsed to this method using the Reflector. Here is its implementation:

public long AvailableFreeSpace
        long num;
        int newMode = Win32Native.SetErrorMode(1);
            long num2;
            long num3;
            if (!Win32Native.GetDiskFreeSpaceEx(this.Name, out num, out num2, out num3))
        return num;

Surprisingly, no WMI here, there is a call to some unmanaged code. Thanks Karl for your insight.

Better And Easier Way To Use WMI

A reminder from the previous post:

“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.”

To overcome this bad API, I recommended WMI Administrative Tools to view WMI classes, properties, qualifiers, and instances. It turns out that there is even a better solution introduced by the Visual Studio Server Explorer. Go to View -> Server Explorer (or press Ctrl + W, L), you can get a list of the available WMI classes under the Management Classes item:


Click on one of the classes and get all of the available properties in the property window. Here are some of the Disk Volumes class properties:


Here is how to get what the WMI Administrative Tools offers, but lets continue and see what else can we achieve. Remember the implementation of getting Windows last Boot uptime from the previous post? Another way of doing that is to right click on the Operating System class and choose Generate Managed Class:


A class named ROOT.CIMV2.Win32_OperatingSystem.cs will be added to your project. This class has all of the Win32_OperatingSystem properties, this time all of the properties are strongly typed. Here is the new get Windows last Boot uptime method:

private static DateTime GetWindowsUptime()
    OperatingSystem0.OperatingSystem0Collection operSysList = OperatingSystem0.GetInstances();
    foreach (OperatingSystem0 operSystem in operSysList)
        // Only demo, so return the first one.
        return operSystem.LastBootUpTime;
    return DateTime.MinValue;

Better, isn’t it? You have normal classes with strongly typed properties, no unboxing needed. Have fun.

Tags :

One Response to “Getting Free Disk Space And WMI Stuff – IMPROVED”

  1. tashuco tumico

    Said on May 22, 2009 :

    Can I make it on a remote pc? ? ?

Post a Comment