且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 WMI 中获取所有用户进程

更新时间:2023-01-30 09:29:57

您可以浏览Win32_Process 类 获取进程详细信息:

you can browse Win32_Process class to get process details:

using System;
using System.Management;
using System.Windows.Forms;

namespace WMISample
{
    public class MyWMIQuery
    {
        public static void Main()
        {
            try
            {
                ManagementObjectSearcher searcher = 
                    new ManagementObjectSearcher("root\\CIMV2", 
                    "SELECT * FROM Win32_Process"); 

                foreach (ManagementObject queryObj in searcher.Get())
                {
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("Win32_Process instance");
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("Caption: {0}", queryObj["Caption"]);
                }
            }
            catch (ManagementException e)
            {
                MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
            }
        }
    }
}

或在 c# 中,不使用 WMI :

or in the c#, without using WMI :

Process[] processlist = Process.GetProcesses();
foreach(Process theprocess in processlist)
  {
    Console.WriteLine("Process: {0} ID: {1}", theprocess.ProcessName, theprocess.Id);
  }