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; 
3
  • 2
    does this help? Commented Nov 20, 2017 at 10:07
  • Thanks a lot Stephan.... Commented Nov 20, 2017 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... Commented Nov 20, 2017 at 11:43

1 Answer 1

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 */ } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.