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;
dsget.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,
where InvokeProcess is more or less the code that you have already given.