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

Resolving the "n timer(s) still in the queue" Error In Angular Unit Tests

Silent Renew and the "login_required" Error When Using oidc-client

How to Get Norton Security Suite Firewall to Allow Remote Desktop Connections in Windows

Fixing the "Please add a @Pipe/@Directive/@Component annotation" Error In An Angular App After Upgrading to webpack 4

How to Determine if a Column Exists in a DataReader