Cool Programming Functionality: Windows Management Instrumentation

In one of the first .NET apps I wrote, I needed to be able to cycle through the CD/DVD drives on the user's system. To determine which logical drives were, in fact, CD/DVD drives, I used the old scripting DLL that came with VB6, available in .NET via a COM wrapper. I am now re-writing this app in C#, and of course wanted to remove the need for this old DLL. I found the solution in the System.Management namespace, which includes functions for the Windows Management Instrumentation infrastructure. Here is some sample code which loops through a system's logical drives and displays information on each. The sample is in VB.NET and requires a reference to the System.Management namespace.

Dim mgmtDisks As New ManagementClass("Win32_LogicalDisk")
Dim moc As ManagementObjectCollection = mgmtDisks.GetInstances()
Dim mo As ManagementObject

For Each mo In moc
   Console.WriteLine("Disk type : {0}", mo("DriveType").ToString())
Next mo
mgmtDisks.Dispose()

Comments

Popular posts from this blog

A Generic Method Using HttpClient to Make a Synchronous Get Request

The Cause and Solution for the "System.Runtime.Serialization.InvalidDataContractException: Type 'System.Threading.Tasks.Task`1[YourTypeHere]' cannot be serialized." Exception

"$.ajax is not a function" Error When Using jQuery and Darren Johnstone's ASP.NET Upload/Download Module v2