问题描述
我打开了五个 notepad.exe.现在,我想知道与每个notepad.exe相关联的所有进程ID.
I have opened five notepad.exe's. Now I want to know all the process id's associated with each notepad.exe.
我知道我可以通过以下方式检索记事本进程列表:
I know I can retrieve list of notepad processes by:
Process []进程= Process.GetProcessesByName("notepad");
Process[] processes = Process.GetProcessesByName("notepad");
但是现在我想要使用LINQ的那些记事本实例的进程ID的string [].我怎么做?我知道我可以创建string []并使用foreach循环填充string [],但我想知道使用LINQ.
But now I want string[] of process Id's of those notepad instances using LINQ. How do I do that? I know i can create string[] and using foreach loop populate the string[] but I would like to know using LINQ.
推荐答案
您可以使用:
Process[] processes = Process.GetProcessesByName("notepad");
string[] ids = processes.Select(p => p.Id.ToString()).ToArray();
但是,我质疑是否需要将其放入 string []
类型.最好保留 Process.Id 值作为整数:
However, I question the need to put this into a string[]
type. It might be better to just leave the Process.Id values as integers:
Process[] processes = Process.GetProcessesByName("notepad");
var ids = processes.Select(p => p.Id);
foreach(int processId in ids)
{
// Do something with each processId
}
这篇关于使用LINQ从进程[]获取字符串[]或进程ID.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!