我在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 = Process.Start("logman.exe", "FabricTraces | findstr Root");
P.WaitForExit();
var result = P.ExitCode;
答案 0 :(得分:0)
这样的事情:
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 */
}