将多个进程的输出连接到 StreamReader
我需要捕获三个进程的输出并将其作为 StreamReader 传递。这在 C# 中可能吗?
背景:我需要具有三个不同参数的另一个文件的输出,但我想处理“完整”输出(所有三个一起)以便在我的代码中进行后续处理。
我应该只调用该过程三次并循环处理输出吗?或者有更有效的方法吗?
目前,我正在这样做
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());
}
,但我还需要来自“dsget”的两个 COUNTRY_XX
组。
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".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有办法,只能调用该过程三次。但是您始终可以将流程调用代码放入接受参数作为输入并返回流程输出的方法中,这样您就不必重复代码。例如,
其中 InvokeProcess 或多或少是您已经给出的代码。
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.