Search code examples
c#processconsoleprocessstartinfo

ProcessStartInfo - print output in console window AND to file (C#)


From my C# application, I am calling Process.Start(myPSI) with the following ProcessStartInfo:

ProcessStartInfo startInfoSigner = new ProcessStartInfo();
startInfoSigner.CreateNoWindow = false;
startInfoSigner.UseShellExecute = false;
startInfoSigner.FileName = pathToMyEXE;
startInfoSigner.WindowStyle = ProcessWindowStyle.Hidden;
startInfoSigner.WindowStyle = ProcessWindowStyle.Minimized;

startInfoSigner.RedirectStandardOutput = true;

This brings up a new console window when running the application, and produces no output (since it is redirected). I read the exe process standard output and write it to a file.

Is there a way to still display the info in this new console window, AND write it to a file (without modifying the pathToMyEXE executable file)?


Solution

  • You need RedirectStandardOutput = true in order to handle the OutputDataReceived Event. In the event handler perform the logging and write the data back to the console.

    private void OutputDataReceived(object sender, DataReceivedEventArgs e) 
    { 
             logger.log(someString);//write to the file
             Console.WriteLine(e.Data);//still display the info in this new console window
    }