c#使用wmi
using System.Management;
ManagementClass mc = new ManagementClass("Win32_Service");
ManagementObjectSearcher sr = new ManagementObjectSearcher("SELECT * FROM Win32_Service WHERE State='Running'");
/*"Stopped","Start Pending","Stop Pending","Running","Continue Pending","Pause Pending","Paused","Unknown"/*
foreach( ManagementObject srv in sr.Get() )
{
Console.Write(srv["DisplayName"].ToString()+ "\n");
}
添加托管类的方法
using WMI.ROOT.CIMV2; //自动生成
ManagementObjectSearcher sr = new ManagementObjectSearcher("SELECT * FROM Win32_Service WHERE State='Running'");
Service s = new Service();
ManagementObjectCollection objs =sr.Get();
Service.ServiceCollection cols = new Service.ServiceCollection(objs);
foreach (Service ss in cols)
{
Console.Write(ss.Caption.ToString() + "\n");
}