Search code examples
c#.netsystem.management

System.Management.ManagementException


I am running following code:

System.Management.ManagementClass wmiNetAdapterConfiguration = new System.Management.ManagementClass("Win32_NetworkAdapterConfiguration");
System.Management.ManagementObjectCollection wmiNetAdapters = wmiNetAdapterConfiguration.GetInstances();
Log.logInfo("Net adapters:" + wmiNetAdapters.get_Count());

And on some machines it is ok, and on some I am getting following error:

System.Management.ManagementException: Not found

Call stack:

System.Management.ManagementException: Not found 
   at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode)
   at System.Management.ManagementScope.InitializeGuts(Object o)
   at System.Management.ManagementScope.Initialize()
   at System.Management.ManagementObject.Initialize(Boolean getObject)
   at System.Management.ManagementClass.GetInstances(EnumerationOptions options)
   at System.Management.ManagementClass.GetInstances()

Any idea why?


Solution

  • The functionality provided by the System.Management namespace is dependent upon the WMI (Windows Management Instrumentation) service.

    I suspect that the WMI service has not been started on the systems that are throwing that exception.

    For troubleshooting purposes, you can verify that using the Administrative Tools → Services utility.

    If this turns out to be the case, you can wrap the code in a try-catch block and use the ServiceController class to start and stop the appropriate service.