开发者

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"));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜