windows c# 一个简单的 WMI 查询中的“无效类”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6918265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c# 'Invalid class' in a simple WMI query
提问by Guillaume V
I will like to find some result after this query, but in the beginning of the foreach loop, the error "invalid class" occur.
我想在这个查询之后找到一些结果,但是在 foreach 循环的开始,出现错误“无效类”。
string wmiQuery = string.Format("SELECT * FROM Win32_Process");
var searcher = new ManagementObjectSearcher(wmiQuery);
var wmiResults = searcher.Get();
foreach (ManagementObject retObject in wmiResults)
{
Console.WriteLine("[{0}]\tName: {1}", retObject["ProcessID"], retObject["Name"]);
}
I use window 7 64, and i wonder if Win32_Process exists. I also use wmi code creator download it from http://www.microsoft.com/downloads/en/details.aspx?familyid=2cc30a64-ea15-4661-8da4-55bbc145c30e&displaylang=enbut i dont find any Win32_Process.
我使用窗口 7 64,我想知道 Win32_Process 是否存在。我也使用 wmi 代码创建者从http://www.microsoft.com/downloads/en/details.aspx?familyid=2cc30a64-ea15-4661-8da4-55bbc145c30e&displaylang=en下载它, 但我没有找到任何 Win32_Process。
Somebody has an idea ?
有人有想法吗?
回答by Guillaume V
I solve my problem. It seem that my WMI was corrupt. After testing WMI with this step:
我解决了我的问题。看来我的 WMI 已损坏。使用此步骤测试 WMI 后:
- Click Start, click Run, type wmimgmt.msc, and then click OK.
- Right-click WMI Control (Local), and then click Properties.
- 单击开始,单击运行,键入 wmimgmt.msc,然后单击确定。
- 右键单击 WMI 控件(本地),然后单击属性。
I saw Win32_Process was a invalid class I follow this step for repair my WMI, and it work
我看到 Win32_Process 是一个无效的类,我按照此步骤修复我的 WMI,它可以工作
1) In the start menu type "cmd"
1)在开始菜单中输入“cmd”
2) Type "net stop winmgmt" and press Enter
2) 输入“net stop winmgmt”并回车
3) Open a Windows Explorer and locate the path to C:\ windows\system32\WBEM\ folder and rename the Repository folder to something else like RepositoryOLD (right click and choose 'Rename Folder').
3) 打开 Windows 资源管理器并找到 C:\ windows\system32\WBEM\ 文件夹的路径,然后将 Repository 文件夹重命名为 RepositoryOLD 之类的其他名称(右键单击并选择“重命名文件夹”)。
4) restart the computer
4)重启电脑
5) In the start menu type "cmd"
5)在开始菜单中输入“cmd”
6) Type "net stop winmgmt" and press enter
6) 输入“net stop winmgmt”并回车
7) Type "winmgmt /resetRepository" and restart the computer.
7) 输入“winmgmt /resetRepository”并重新启动计算机。
回答by Igor Turman
As dominus suggested, use the Process class:
正如 dominus 建议的那样,使用 Process 类:
...
Process[] processes = Process.GetProcesses();
foreach (Process process in processes)
Console.WriteLine(process.ProcessName);
...
回答by Michiel de Wolde
In my opinion the exception does not sufficiently state the problem. The code below works. If you for example omit "\cimv2" in the scope the exception occurs. If you for example erroneously select from "Win32_Processes" the exception also occurs. Hence at least make sure the scope is correct and the query is correct.
在我看来,这个例外并没有充分说明问题。下面的代码有效。例如,如果您在范围中省略“\cimv2”,则会发生异常。例如,如果您错误地从“Win32_Processes”中选择,也会发生异常。因此,至少要确保范围正确且查询正确。
ManagementScope scope = new ManagementScope(@"\localhost\root\cimv2");
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_Process");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
foreach (ManagementBaseObject eachObject in searcher.Get())
{
Console.WriteLine("Value: {0}", eachObject);
}