How do you generate C# classes for accessing WMI?
3 Answers
To generate strongly typed WMI classes, use the Management Strongly typed class generator (MgmtClassGen.exe). It's usually in C:\Program Files\Microsoft Visual Studio X\SDK\vX\Bin. The parameters are at MSDN and they even have a page describing the code generated. If you have to do a lot of work with WMI, it's a lifesaver.
-
1on more recent setups where the SDK is separate the path is a little different e. g.
c:\Program Files (x86)\Microsoft sdks\windows\v8.0a\bin\NET FX 4.0 Tools
Oct 24, 2013 at 18:03 -
@MikeBaz and @VanOrman - see my comment above - do you know perhaps how to use it with
Visual Studio 2015
?– TarApr 18, 2016 at 6:22 -
"C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6 Tools\mgmtclassgen.exe" "C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\mgmtclassgen.exe" "C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\mgmtclassgen.exe" "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\mgmtclassgen.exe" "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\mgmtclassgen.exe" "C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\mgmtclassgen.exe"– AMissicoJun 24, 2016 at 4:27
Easier approach (Visual Studio users):
Add WMI classes to VisualStudio's Server Explorer. E.g.
Get Visual Studio to call MgmtClassGen.exe for you. E.g.
@VanOrman provides additional references for MgmtClassGen.exe.
-
Sorry for commenting 2+ years later - I was occupied searching how to use the underlying tool, the
MgmtClassGen.exe
inVS
. such a shame, 2+ years were wasted. Since I don't have theManagement Classes
entry under my machine, maybe do you have an idea how to add it toVS2015
? I suppose I should have theMgmtClassGen.exe
somehow connected under the hood for that...? (the Management (WMI) Extensions for Visual Studio .NET 2003 Server Explorer, as its name suggests, is not forVisual Studio 2015
)– TarApr 18, 2016 at 6:12 -
1Any idea how to get this menu item in vs 2019? I see the event logs tree and such in Server Explorer and MgmtClassGen.exe is on my machine but I don't see the nice UI– TimFeb 6, 2020 at 7:49
You can try WMICodeCreator it generates vbscript jscript vb.net and c# code Download WMICodeCreator from Microsoft