WMI error with a simple query in C#

时光怂恿深爱的人放手 提交于 2019-12-24 08:39:48

问题


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.


回答1:


Sorry everyone. I just realized that the "CommandLine" field is only in Windows XP and higher. Problem solved.



来源:https://stackoverflow.com/questions/1474551/wmi-error-with-a-simple-query-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!