2

I am trying to execute a file stored under program files. The program is usually called from a command prompt, and output information while it is running. I want to capture the output.

The code below solves half of the job: The program is executed, and the console pops up showing the output in the console, but I cannot capture the output:

ProcessStartInfo start = new ProcessStartInfo();
start.WorkingDirectory = directory;
start.FileName = fileName;
start.Arguments = arguments;
//start.UseShellExecute = false;
//start.RedirectStandardOutput = true;

using (Process process = Process.Start(start))
{
   // code
}

Now I am using the code below to capture the output:

ProcessStartInfo start = new ProcessStartInfo();
start.WorkingDirectory = directory;
start.FileName = fileName;
start.Arguments = arguments;
start.UseShellExecute = false;
start.RedirectStandardOutput = true;

using (Process process = Process.Start(start))
{
   using (StreamReader reader = process.StandardOutput)
   {
      string result = reader.ReadToEnd();
      Console.Write(result);
   }
}

The program crashes in the line where I execute the start command with the error "The system cannot find the file specified"

What I don't get is, the first part of the program works, but not the second.

I have search a lot on this topic, and there are many suggestions but have not been able to find one which solves my issue.

I am using .NET 3.5 for the program which I prefers due to compatibility with other parts of the program.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
MrMox
  • 29
  • 1
  • 1
    Is `.FileName` value a full path or file in the current working directory? If not, that's likely the issue. Note that it has no relationship to `.WorkingDirectory` as it is working directory for process to be created. – LB2 Mar 10 '14 at 22:11
  • There's a full example of setting up a 'Process' for capturing output over there http://stackoverflow.com/questions/18338397/start-and-manage-external-process-from-a-wpf-app/18342744#18342744 – Gayot Fow Mar 10 '14 at 22:14
  • Thanks LB2 - It solved the issue and it now picks up the output. I have spend the last 3 hours looking at this... :) – MrMox Mar 10 '14 at 22:17

0 Answers0