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.
我有一个服务,它创建了许多子进程。使用c#我需要确定当前正在运行的这些子进程的数量。
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.
例如,我有一个名为“TheService”的服务。这会产生5个子进程,全部称为“process.exe”。是否可以确定在服务下运行的子进程数?基本上我需要知道“process.exe”的实例数,只给出服务/服务进程名称的名称。
2 个解决方案
#1
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:
您需要使用WMI,Win32_Process类包含父进程ID。所以WQL查询(参见.NET下WMI的System.Management命名空间)如:
SELECT * FROM Win32_Process Where ParentProcessId = n
replacing n with the service's process id.
用服务的进程ID替换n。
EDIT Sample code (based on code by Arsen Zahray):
编辑示例代码(基于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;
}
#2
I am not sure exactly what you mean by "the name of the service" - would that be process.exe?
我不确定你所说的“服务名称”是什么意思 - 那会是process.exe吗?
If so, the static method Process.GetProcessesByName() should do the trick:
如果是这样,静态方法Process.GetProcessesByName()应该做的伎俩:
Process[] procs = Process.GetProcessesByName("process");
Console.WriteLine(procs.Length);
Let me know if I misunderstood your question.
如果我误解了你的问题,请告诉我。
#1
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:
您需要使用WMI,Win32_Process类包含父进程ID。所以WQL查询(参见.NET下WMI的System.Management命名空间)如:
SELECT * FROM Win32_Process Where ParentProcessId = n
replacing n with the service's process id.
用服务的进程ID替换n。
EDIT Sample code (based on code by Arsen Zahray):
编辑示例代码(基于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;
}
#2
I am not sure exactly what you mean by "the name of the service" - would that be process.exe?
我不确定你所说的“服务名称”是什么意思 - 那会是process.exe吗?
If so, the static method Process.GetProcessesByName() should do the trick:
如果是这样,静态方法Process.GetProcessesByName()应该做的伎俩:
Process[] procs = Process.GetProcessesByName("process");
Console.WriteLine(procs.Length);
Let me know if I misunderstood your question.
如果我误解了你的问题,请告诉我。