我怎样才能获得子进程的列表,在C#中的给定服务队? [英] How can I get a list of child processes for a given sevice in C#?

查看:117
本文介绍了我怎样才能获得子进程的列表,在C#中的给定服务队?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有创建许多子进程的服务。使用C#我需要确定这是目前运行这些子进程的数量。

I have a service which creates a number of child processes. Using c# I need to determine the number of these child processes which are currently running.

例如我有一个服务运行名为TheService。这5派生的子进程,全称为process.exe。是否有可能确定服务下运行的子进程的数目?基本上我需要知道给定的服务/服务进程名称的唯一的名称为process.exe的实例的数量。

For example I have a service running called "TheService". This spawns 5 child processes, all called "process.exe". Is it possible to determine the number of child processes running under the service? Essentially I need to know the number of instances of "process.exe" given only the name of the service/service process name.

推荐答案

您需要使用WMI,在 Win32_Process的类包括父进程ID。因此,一个WQL查询(参见下.NET System.Management命名空间的WMI),如:

You need to use WMI, the Win32_Process class includes the parent process id. So a WQL query (see System.Management namespace for WMI under .NET) like:


SELECT * FROM Win32_Process Where ParentProcessId = n

与该服务的进程ID替换的 N

replacing n with the service's process id.

编辑示例代码(由阿尔森Zahray 基于代码):

EDIT Sample code (based on code by Arsen Zahray):

static List<Process> GetChildPrecesses(int parentId) {
  var query = "Select * From Win32_Process Where ParentProcessId = "
          + parentId;
  ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
  ManagementObjectCollection processList = searcher.Get();

  var result = processList.Select(p =>
    Process.GetProcessById(Convert.ToInt32(p.GetPropertyValue("ProcessId")));
  ).ToList();

  return result;
}

这篇关于我怎样才能获得子进程的列表,在C#中的给定服务队?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆