使用下面的 WMI 查询我可以获得所有服务名称,
ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_Service ")
此外,当我在命令提示符下运行以下命令时,它将给出所有进程 ID (PID) 和服务名称,
tasklist /svc /fi "imagename eq svchost.exe"
我想要 WMI/C# 方式来查找“svchost.exe”进程下运行的所有服务?
除了WMI还有其他方法吗?
最佳答案
我将创建一个批处理文件,用 C# 触发该文件并捕获返回值 列表中的。
解决方案可能如下所示:
myBatch.bat:
tasklist /svc /fi "IMAGENAME eq svchost.exe"
C# 程序:
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "myBatch.bat";
p.Start();
string output = p.StandardOutput.ReadToEnd();
Console.Write(output);
p.WaitForExit();
关于c# - 如何获取 "svchost.exe"进程下运行的所有服务名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46588575/