C# 中的简单查询导致 WMI 错误
我尝试在 Windows 2000 上使用此代码:
foreach (Process p in Process.GetProcesses())
{
if (p.MainModule.FileName.EndsWith("calc.exe"))
{
using (ManagementObjectSearcher mos =
new ManagementObjectSearcher(
"SELECT CommandLine,ExecutablePath
FROM Win32_Process WHERE ProcessId=" + p.Id.ToString()))
{
using (ManagementObjectCollection moc = mos.Get())
{
foreach (ManagementObject mo in moc)
{
MessageBox.Show((string)mo["CommandLine"]);
return;
}
}
}
}
}
这适用于 Windows XP 及更高版本,但在 Windows 2000 上失败,并出现“无效查询”错误。 根据 MSDN,Win32_Process 对象是Windows 2000 及更高版本支持,所以我不确定我做错了什么。任何帮助将不胜感激。
I am trying to use this code on Windows 2000:
foreach (Process p in Process.GetProcesses())
{
if (p.MainModule.FileName.EndsWith("calc.exe"))
{
using (ManagementObjectSearcher mos =
new ManagementObjectSearcher(
"SELECT CommandLine,ExecutablePath
FROM Win32_Process WHERE ProcessId=" + p.Id.ToString()))
{
using (ManagementObjectCollection moc = mos.Get())
{
foreach (ManagementObject mo in moc)
{
MessageBox.Show((string)mo["CommandLine"]);
return;
}
}
}
}
}
This works on Windows XP and higher, but fails on Windows 2000 with an "Invalid query" error. According to MSDN, the Win32_Process object is supported on Windows 2000 and higher, so I'm not sure what I'm doing wrong. Any help would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对不起大家。我刚刚意识到“CommandLine”字段仅在 Windows XP 及更高版本中存在。问题解决了。
Sorry everyone. I just realized that the "CommandLine" field is only in Windows XP and higher. Problem solved.