我有 C# 代码试图从 WMI 对象加载一些属性 - Win32_Service。但它返回给我 'System.Management.ManagementException: Invalid class' 错误。
我的简化代码:
static void Main(string[] args)
{
string serviceName = "AppFabricEventCollectionService";
string propertyName = "StartName";
var obj = GetProperty(serviceName, propertyName);
}
private static string GetProperty(string serviceName, string propertyName)
{
using (ManagementObject obj2 = GetWindowsServiceManagementObject(serviceName))
{
return (obj2.GetPropertyValue(propertyName) as string);
}
}
public static ManagementObject GetWindowsServiceManagementObject(string serviceName)
{
return new ManagementObject(string.Format("Win32_Service.Name='{0}'", serviceName));
}
我还尝试通过 PowerShell 命令加载 WMI 对象列表 -
获取 WmiObject -List | 选择名称
它返回 91 个对象,但它错过了 Win32_Service 对象。我用谷歌搜索了如何重新安装它,但没有找到。有没有办法以某种方式重新安装它?
谢谢
更新 1: Powershell 命令的输出:
PS C:\Windows\system32> Get-WmiObject Win32_Service
Get-WmiObject : Invalid class "Win32_Service"
At line:1 char:1
+ Get-WmiObject Win32_Service
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidType: (:) [Get-WmiObject], ManagementExce
ption
+ FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.C
ommands.GetWmiObjectCommand