gpt4 book ai didi

c# - 缺少 Win32_Service

转载 作者:可可西里 更新时间:2023-11-01 11:55:24 26 4
gpt4 key购买 nike

我有 C# 代码尝试从 WMI 对象加载某些属性 - Win32_Service。但它返回给我“System.Management.ManagementException:无效类”错误。

我的简化代码:

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-列表 |选择名称

它返回 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

最佳答案

尝试重新注册基类(当然是管理命令行...)

mofcomp %windir%\system32\wbem\cimwin32.mof

关于c# - 缺少 Win32_Service,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18408714/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com