process.getprocessesbyname()
I would like to use this function in C#, but I need 开发者_如何学编程to get 2 types of processes. Is it possible to do something like this: process.getprocessesbyname("process1", "process2");
How can I get the instances of 2 processes with different names?
TY
IEnumerable<Process> processes =
new[] { "process1", "process2" }
.SelectMany(Process.GetProcessesByName);
Use a loop:
static void Main(string[] args)
{
var processes = GetProcesses(new[] { "name1", "name2", "explorer"});
}
public static IList<Process> GetProcesses(string[] processNames)
{
var processes = new List<Process>(processNames.Length);
foreach (var processName in processNames)
{
var namedProcesses = Process.GetProcessesByName(processName);
processes.AddRange(namedProcesses);
}
return processes;
}
You can use the LINQ Concat
method to concatenate the lists:
var processes = Process.GetProcessesByName("devenv")
.Concat(Process.GetProcessesByName("notepad"));
精彩评论