Redirecting StandardError and StandardOutput of Process

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I've seen a number of posts related to this subject. I have written a C# Win Form wrapper for FC.EXE. It works great when launching from the Visual Studio IDE. However, when launching the compiled executable, I am unable to get the StandardError and StandardOutput of the Process. The Process, and my C# application hangs. Why does it work in debug mode but not in non-debug mode? Is this related to the deadlock situation I have read about where it is recomended to use separate threads to read from the StandardError and StandardOutput streams? Following is my code for the method that starts the process.

private string CompareFiles(string File1, string File2)
string qoute = ((char)34).ToString()
System.Text.StringBuilder sbArgs = new System.Text.StringBuilder()

Process myProc = new Process()
myProc.StartInfo.FileName = @"fc.exe"

// Append switches for FC.ex
if(chkBinary.Checked)
sbArgs.Append(@"/B ")

else
if(chkFirstLastLine.Checked) sbArgs.Append(@"/A ")
if(chkIgnoreCase.Checked) sbArgs.Append(@"/C ")
if(chkASCII.Checked) sbArgs.Append(@"/L ")
if(chkLineNumbers.Checked) sbArgs.Append(@"/N ")
if(chkDoNotExpandTabs.Checked) sbArgs.Append(@"/T ")
if(chkUnicode.Checked) sbArgs.Append(@"/U ")
if(chkCompressWhiteSpace.Checked) sbArgs.Append(@"/W ")


// Append Filename
sbArgs.Append(qoute + File1 + qoute + " " + qoute + File2 + qoute)

// finish preparing the process and start i
myProc.StartInfo.Arguments = sbArgs.ToString()
myProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
myProc.StartInfo.UseShellExecute = false
myProc.StartInfo.RedirectStandardOutput = true
myProc.StartInfo.RedirectStandardError = true

myProc.Start()

// Collect the erro
string error = myProc.StandardError.ReadToEnd()

// Collect the outpu
string output = myProc.StandardOutput.ReadToEnd()
MessageBox.Show(output)

// Wait for the process to exit before doing anythin
myProc.WaitForExit()

// Free resource
myProc.Close()

return error + output
 
Back
Top