2

in my project (MVC 3) I want to run an external console application using the following code:

   string returnvalue = string.Empty;

   ProcessStartInfo info = new ProcessStartInfo("C:\\someapp.exe");
   info.UseShellExecute = false;
   info.Arguments = "some params";
   info.RedirectStandardInput = true;
   info.RedirectStandardOutput = true;
   info.CreateNoWindow = true;

   using (Process process = Process.Start(info))
   {
      StreamReader sr = process.StandardOutput;
      returnvalue = sr.ReadToEnd();
   }

but I get an empty string in the returnvalue and that program creates a file as a result but there is not any file created. Maybe taht Process is not being executed ?

7
  • 1
    could it be piping it out to standard error? Commented Apr 4, 2013 at 15:43
  • Does your IIS apppool user has enough rights? Commented Apr 4, 2013 at 15:44
  • Not enough information. There is not file being created here and your questions are unclear. Commented Apr 4, 2013 at 15:45
  • If you do this, be aware that your site won't work in Medium Trust so you won't be able to host it most of the shared hosting environment companies. Commented Apr 4, 2013 at 15:47
  • 1
    @Tony -> RedirectStandardError = true and then var errorReader = process.StandardError -> read that you might get error output from the application Commented Apr 4, 2013 at 15:49

3 Answers 3

3

If I recall correctly, to read both standard error and standard output at the same time, you must do it with an async callback:

var outputText = new StringBuilder();
var errorText = new StringBuilder();
string returnvalue;

using (var process = Process.Start(new ProcessStartInfo(
    "C:\\someapp.exe",
    "some params")
    {
        CreateNoWindow = true,
        ErrorDialog = false,
        RedirectStandardError = true,
        RedirectStandardOutput = true,
        UseShellExecute = false
    }))
{
    process.OutputDataReceived += (sendingProcess, outLine) =>
        outputText.AppendLine(outLine.Data);

    process.ErrorDataReceived += (sendingProcess, errorLine) =>
        errorText.AppendLine(errorLine.Data);

    process.BeginOutputReadLine();
    process.BeginErrorReadLine();
    process.WaitForExit();
    returnvalue = outputText.ToString() + Environment.NewLine + errorText.ToString();
}
Sign up to request clarification or add additional context in comments.

Comments

0

as TimothyP said in the comment, after setting RedirectStandardError = true and then via process.StandardError.ReadToEnd() I get the error message content

Comments

0

You have to wait for your external program to finish otherwise the output you want to read is not even generated when you want to read it.

using (Process process = Process.Start(info))
{
  if(process.WaitForExit(myTimeOutInMilliseconds))
  {
  StreamReader sr = process.StandardOutput;
  returnvalue = sr.ReadToEnd();
  }
}

1 Comment

process.WaitForExit() it's a void function, not bool

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.