Getting drive and mount point information using WMI

I had to get volume and mount point information for a remote system using WMI. Although I had heard of the term “mount point” before, I had never done anything with that as my sys admin skills are not anywhere close to where my programming ones are. So after getting a quick tutorial from Google and Mikey, I was able to get going.  Basically, a mount point is when you take a drive on your machine and instead of assigning it a drive letter, you assign it a directory within an existing drive.  So instead of having C:, D:, E: drives, you would have something like C:, C:\MyMountedPoints\D, C:\MyMountedPoints\E, or something maybe a little bit friendlier.  It keeps your users from having to know the structure of your server plus you can go beyond the alphabet in number of drives.

So anyways, now the interesting part was getting the drive information.  Win32_LogicalDisk only gives you the drives that are mapped to a letter, so it doesn’t even see the mounted ones.  So I used Win32_Volume and checked for the ones that don’t have a DriveLetter.  Then to get further information about the directory, i ran a query against Win32_MountPoint and got what I needed.  I could’ve just ran the query against Win32_MountPoint but I had some data in Volume that I needed.  The tricky part was querying Win32_MountPoint since the data that it contains is of type Win32_Directory and Win32_Volume, so you have to include a bunch of escape characters to get the syntax just right…WML is very fuzzy when it comes to that and the debugging tools are really not up to par.  Here’s the code (it’s very watered down as I stripped out all the business logic that my client wouldn’t want me to post).

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string scopeStr = string.Format(@"\\{0}\root\cimv2", "TestSqlServer");

            
            ManagementScope scope = new ManagementScope(scopeStr);
            scope.Connect();

            string queryString = "SELECT * FROM Win32_Volume WHERE DriveLetter IS NULL";
            SelectQuery query = new SelectQuery(queryString);
            using (
                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher(scope, query))
            {
                foreach (ManagementObject disk in searcher.Get())
                {
                    string _MountPoint = disk["Name"].ToString();
                    if (_MountPoint[_MountPoint.Length - 1] == Path.DirectorySeparatorChar)
                    {
                        _MountPoint = _MountPoint.Remove(_MountPoint.Length - 1);
                    }
                    _MountPoint = _MountPoint.Replace("\\", "\\\\\\\\");

                    string _MountPointQueryString = "select * FROM Win32_MountPoint WHERE Directory=\"Win32_Directory.Name=\\\"" + _MountPoint + "\\\"\"";

                    SelectQuery _MountPointQuery = new SelectQuery(_MountPointQueryString);
                    using (
                        ManagementObjectSearcher mpsearcher =
                            new ManagementObjectSearcher(scope, _MountPointQuery))
                    {
                        foreach (ManagementObject mp in mpsearcher.Get())
                        {

                            try
                            {
                                
                                Console.WriteLine(mp["Volume"].ToString());
                                Console.WriteLine(mp["Directory"].ToString());
                                Console.WriteLine();

                            }
                            catch { }
                        }
                    }
                }
            }
        }
    }
}

Scroll to Top