Finding all local drives using WMI in C# .NET

WMI – Windows Management Instrumentation – provides a set of tools to monitor the system resources, such as devices and applications. WMI is represented by the System.Management library that you set a reference to in a .NET project.

We’ll quickly look at how to enumerate all local drives on a computer in your network. You might need to run Visual Studio as an administrator:

ConnectionOptions connectionOptions = new ConnectionOptions();
connectionOptions.Username = "andras.nemes";
connectionOptions.Password = "p@ssw0rd";
			
ManagementPath managementPath = new ManagementPath();
managementPath.Path = "\\\\machinename\\root\\cimv2";

ManagementScope managementScope = new ManagementScope(managementPath, connectionOptions);

ObjectQuery objectQuery = new ObjectQuery("SELECT Size, Name FROM Win32_LogicalDisk where DriveType=3");

ManagementObjectSearcher objectSearcher = new ManagementObjectSearcher(managementScope, objectQuery);
ManagementObjectCollection objectCollection = objectSearcher.Get();

foreach (ManagementObject managementObject in objectCollection)
{
	Console.WriteLine("Resource name: {0}", managementObject["Name"]);
	Console.WriteLine("Resource size: {0}", managementObject["Size"]);
}

First we set up the user information to access the other computer with the ConnectionOptions object. Then we declare the path to the WMI namespace where the resource exists. In this case it’s “root\\cimv2”.

Further down you’ll see how to list all WMI namespaces.

Next we build a ManagementScope object using the management path and connection options inputs. Then comes the interesting part: an SQL-like syntax to query the resource, in this case Win32_LogicalDisk. This page lists all selectable properties of Win32_LogicalDisk including the values for DriveType.

ManagementObjectSearcher is the vehicle to run the query on the declared scope. The Get() method of ManagementObjectSearcher enumerates all management objects that the query returned. The loop prints the properties that we extracted using the query.

Here’s how you can print the WMI namespaces of a root:

private static List<String> GetWmiNamespaces(string root)
{
	List<String> namespaces = new List<string>();
	try
	{
		ManagementClass nsClass = new ManagementClass(new ManagementScope(root), new ManagementPath("__namespace"), null);
		foreach (ManagementObject ns in nsClass.GetInstances())
		{
			string namespaceName = root + "\\" + ns["Name"].ToString();
			namespaces.Add(namespaceName);
			namespaces.AddRange(GetWmiNamespaces(namespaceName));
		}
	}
	catch (ManagementException me)
	{
		Console.WriteLine(me.Message);
	}

	return namespaces;
}

If you’d like to enumerate the WMI namespaces on your local PC then you can call this function like…

List<String> namespaces = GetWmiNamespaces("root");

You can view all posts related to Diagnostics here.

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

ultimatemindsettoday

A great WordPress.com site

iReadable { }

.NET Tips & Tricks

Robin Sedlaczek's Blog

Love to use Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Ricos Blog zu Softwaredesign- und architektur

Ideen und Gedanken rund um Softwaredesign und -architektur, Domain-Driven Design, C# und Windows Azure

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: