tags:

views:

48

answers:

3

Hello,

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

+2  A: 

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;
}
simendsjo
+1  A: 

You can use the LINQ Concat method to concatenate the lists:

var processes = Process.GetProcessesByName("devenv")
    .Concat(Process.GetProcessesByName("notepad"));
Fredrik Mörk
I don't have linq. I'm using framework 2.0.
+2  A: 
IEnumerable<Process> processes =
    new[] { "process1", "process2" }
    .SelectMany(Process.GetProcessesByName);
Darin Dimitrov
I don't have linq. I'm using framework 2.0.