本文告诉大家如何通过 WMI 获取系统启动的服务
通过 Win32_Service 可以获取系统启动的服务
获取的时候只需要拿Caption和State就可以
代码语言:javascript复制 var mc = "Win32_Service";
var managementObject = new[]
{
"Caption",
//"CreationClassName",
//"Description",
//"DisplayName",
//"ErrorControl",
//"Name",
//"PathName",
//"ServiceType",
//"StartMode", // 是否开机启动
//"StartName",
"State", // 是否在运行
//"Status",
//"SystemCreationClassName",
//"SystemName",
};
ManagementClass managementClass = new ManagementClass(mc);
ManagementObjectCollection managementObjectCollection = managementClass.GetInstances();
var str = new StringBuilder();
foreach (ManagementObject m in managementObjectCollection)
{
foreach (var temp in managementObject)
{
try
{
str.Append(temp);
str.Append(" ");
str.Append(m[temp]?.ToString() ?? "");
str.Append("n");
}
catch (Exception e)
{
Console.WriteLine(temp " " e);
}
}
str.Append("n");
}
return str.ToString();
输出 str 的内容
代码语言:javascript复制Caption : Apple Mobile Device Service
State : Running
Caption : iPod 服务
State : Running
上面只是输出的一点,因为服务是很多
Win32_Service class - Windows applications