Concatenate output of multiple processes to StreamReader
I need to capture the ouput of three processes and pass it as a StreamReader
. Is this possible in C#?
Background: I need the output of another file with three different arguments, but I want to treat the "complete" output (all three together) for latter processing in my code.
Should I just call the process three times and process the output in a loop? Or is there a more efficient way?
Currently, I'm doing it like this
if (String.IsNullOrEmpty(file))
{
Process dsget;
dsget = Process.Start("dsget", "group \"CN=COUNTRY_DE,DC=cms,DC=local\" -members");
dsget.StartInfo.UseShellExecute = false;
dsget.StartInfo.RedirectStandardOutput = true;
dsge开发者_Go百科t.Start();
reader = dsget.StandardOutput;
}
else
{
reader = new StreamReader(file);
}
while ((line = reader.ReadLine()) != null)
{
if (!line.Contains("CN"))
continue;
string username = line.Replace("\"", "").Split(',')[0].Split('=')[1];
countryUsers.Add(username.ToUpperInvariant());
}
But I need two more COUNTRY_XX
-groups from "dsget".
There is no other way but to call the process three times. But you can always put process invocation code into a method that accepts argument as input add return the process output so you don't have to repeat the code. For example,
var info = new StringBuilder();
info.Append(InvokeProcess(argument1));
info.Append(InvokeProcess(argument2));
info.Append(InvokeProcess(argument3));
where InvokeProcess is more or less the code that you have already given.
精彩评论