Creating Strongly-Typed WMI Classes with Mgmtclassgen.exe
My recent work involved writing a C# to automating the installs of server products. A lot of the work involved using WMI.
Generally, working with WMI in scripting languages like Perl, Python, or VBScript is easy but using it in C# can be difficult. WMI is very late-bound and that can be an unpleasant burden with an early-bound language like C#.
Mgmtclassgen.exe makes it simple
Mgmtclassgen.exe creates stringly-typed wrapper "C#" wrapper classes for WMI classes. It has made a substantial amount of my WMI work very easy.
A simple snippet:
foreach(Process ps in Process.GetInstances())
{
Console.WriteLine(ps.Name);
}
How to get Mgmtclassgen.exe
Search in your "Program Files" folder for your version of visual studio and you should find Mgmtclassgen.exe. I've found it in Visual Studio 2003 and Visual Studio 2005.
How to use Mgmtclassgen.exe
Here are the MSDN docs for mgmtclassgen.exe
A simple example:
C:> mgmtclassgen.exe Win32_Service
This will generate a .CS file containing the strongly-typed wrapper for the Win32_Service class