简体   繁体   中英

WMI error with a simple query in C#

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM