如何将流程的输出捕获到C#变量中?
我在windows命令提示符下运行下面的命令,它给出如下输出如何将流程的输出捕获到C#变量中?,c#,cmd,system.diagnostics,C#,Cmd,System.diagnostics,我在windows命令提示符下运行下面的命令,它给出如下输出 C:\>logman.exe FabricTraces | findstr Root Root Path: C:\ProgramData\Windows Fabric\Fabric\log\Traces\ 现在,我试图在C程序中模拟相同的情况,并希望将输出(C:\ProgramData\Windows Fabric\Fabric\log\Traces\)捕获到一个变量中 如何做到这一点,这是我试过的代码 Process P =
C:\>logman.exe FabricTraces | findstr Root
Root Path: C:\ProgramData\Windows Fabric\Fabric\log\Traces\
现在,我试图在C程序中模拟相同的情况,并希望将输出(C:\ProgramData\Windows Fabric\Fabric\log\Traces\
)捕获到一个变量中
如何做到这一点,这是我试过的代码
Process P = Process.Start("logman.exe", "FabricTraces | findstr Root");
P.WaitForExit();
var result = P.ExitCode;
大概是这样的:
private void StartProcess()
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = /* path + binary */;
process.StartInfo.Arguments = /* arguments */;
process.StartInfo.WorkingDirectory = /* working directory */;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.OutputDataReceived += Process_OutputDataReceived;
process.ErrorDataReceived += Process_ErrorDataReceived;
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
}
private void Process_ErrorDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e)
{
/* e.Data will contain string with error message */
}
private void Process_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e)
{
/* e.Data will contain string with output */
}
有帮助吗?非常感谢Stephan…如果要使用管道和其他外壳功能,您必须使用
/C
选项启动cmd
FabricTraces | findstr Root
不是进程的参数字符串。。。