0

I am running below command in windows command prompt and it's giving output like below,

C:\>logman.exe FabricTraces | findstr Root
Root Path: C:\ProgramData\Windows Fabric\Fabric\log\Traces\

Now, I am trying to mimic the same in C# program and would like to capture the output (C:\ProgramData\Windows Fabric\Fabric\log\Traces\) into a variable.

How to do this, here's the code I tried,

Process P = Process.Start("logman.exe", "FabricTraces | findstr Root");
            P.WaitForExit();
            var result = P.ExitCode;
user584018
  • 10,186
  • 15
  • 74
  • 160
  • 2
    does [this](https://stackoverflow.com/questions/206323/how-to-execute-command-line-in-c-get-std-out-results?noredirect=1&lq=1) help? – Stephan Nov 20 '17 at 10:07
  • Thanks a lot Stephan.... – user584018 Nov 20 '17 at 10:15
  • You have to start `cmd` with the `/C` option if you want to use pipes and other shell features. `FabricTraces | findstr Root` isn't the argument string of the process... – IS4 Nov 20 '17 at 11:43

1 Answers1

0

Something like this:

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 */
}
Avo Nappo
  • 620
  • 4
  • 9