Posts

Showing posts from December, 2005

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()

Cool Programming Functionality: Reflection

In Microsoft's .NET Framewowk, Reflection describes the ability to interpret types dynamically at runtime, or, as the documentation puts it: "The classes in the Reflection namespace, together with System.Type, allow you to obtain information about loaded assemblies and the types defined within them, such as classes, interfaces, and value types. You can also use reflection to create type instances at run time, and to invoke and access them." I've been making use of this lately, and wondering how I could have solved certain tasks without it. Here's a quick example function in C# that takes a namespace name, class name, and method name as parameters, then instantiates the specified class and calls the specified method. This is just a quickie, so I'm forgoing a lot of things like error handling, etc. The code itself is mine, but the architecture behind it is from a colleague of mine named Bill Butler ( Xcent ). public void ExecObjectMethod( string NamespaceName,