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()
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
Post a Comment