WMI error with a simple query in C#

2019-08-01 08:18发布

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.

标签: c# wmi
1条回答
姐就是有狂的资本
2楼-- · 2019-08-01 08:43

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

查看更多
登录 后发表回答