System.Management.ManagementException

我正在运行以下代码:

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

在一些机器上它没关系,有些我得到以下错误:

System.Management.ManagementException:未找到

调用堆栈:

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

知道为什么吗?

System.Management命名空间提供的function取决于WMI(Windows Management Instrumentation)服务。

我怀疑WMI服务尚未在抛出exception的系统上启动。

出于疑难解答的目的,您可以使用管理工具→服务实用程序validation。

如果事实certificate是这种情况,您可以将代码包装在trycatch块中,并使用ServiceController类来启动和停止相应的服务。

我们刚刚遇到了同样的问题,并且一步一步地按照下面的链接后的post,并且能够成功解决问题。

https://answers.microsoft.com/en-us/windows/forum/all/systemmanagementmanagementexception-invalid/74ac22c7-509d-42a5-9f7f-2686dc87b7b2

基本上,打开计算机管理,在左侧的侧栏中转到服务和应用程序下拉菜单,然后转到WMI控件。 然后查看右侧的栏,然后单击更多操作,然后单击属性。 如果您遇到与我相同的错误,您会在此处看到它。 我尝试过“从上次备份恢复”,它出现了一些错误。 然后我点击’立即恢复’…我没有备份,但我看到备份文件被称为’* .rec files’。 我去了Windows搜索并搜索了* .rec,它在C:\ Windows \ System32 \ wbem中找到了一个。 它被称为corrupted.rec所以我没有太多的希望,似乎它提出了与以前相同的信息。 所以我继续环顾四周,直到我最终回到WMI控件,发现它不再显示命名空间错误,而是我的系统规范。 在我看到这个之后,我打开了控制笔记本电脑和IT打开的灯的软件。 我终于能够把我该死的令人讨厌的笔记本电脑灯关掉了。 在此之后,我为WMI做了备份,现在我将创建一个系统还原点。