locked
How do I redirect standard I/O? RRS feed

Answers

  • For a detailed introduction, please refer to this article by Manish Ranjan Kumar:
    http://www.codeproject.com/KB/cs/ProcessStartDemo.aspx

    Here is the code sample:

     static void Main(string[] args)
            {
                StreamReader outputReader = null;
                StreamReader errorReader = null;
    
                try 
                {
                    ProcessStartInfo psi = new  ProcessStartInfo(@"c:\Program.exe", "a b c");
    
                    psi.ErrorDialog = false;
                    psi.UseShellExecute = false;
                    psi.RedirectStandardError = true;
                    psi.RedirectStandardInput = true;
                    psi.RedirectStandardOutput = true;
    
                    Process proc = new Process();
                    proc.StartInfo = psi;
                    bool started = proc.Start();
                    if (started)
                    {
                        outputReader = proc.StandardOutput;
                        errorReader = proc.StandardError;
                        proc.WaitForExit();
    
                        string displayText = "Output" + Environment.NewLine;
                        displayText += outputReader.ReadToEnd();
                        displayText += Environment.NewLine + "Error" + Environment.NewLine;
                        displayText += errorReader.ReadToEnd();
    
                        Console.WriteLine(displayText);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                finally
                {
                    if(outputReader != null)
                    {
                        outputReader.Close();
                    }
                    if(errorReader != null)
                    {
                        errorReader.Close();
                    }
                }
            }
    
    

     

    Suppose application A needs some arguments to be input one by one. In order to run application A in application B, we can redirect the standard input.

    For instance, here is the code for application A:

     static void Main(string[] args)
            {
                string input1 = Console.ReadLine();
                string input2 = Console.ReadLine();
    
                Console.WriteLine(input1);
                Console.WriteLine(input2);  
            }
    
    

     

    Here is the code for application B:

     static void Main(string[] args)
            {
                ProcessStartInfo psi = new ProcessStartInfo();
                psi.FileName = @"c:\applicationA.exe";
                psi.UseShellExecute = false;
                psi.RedirectStandardInput = true;
                Process proc = new Process();
                proc.StartInfo = psi;
                proc.Start();
    
                proc.StandardInput.WriteLine(10);
                proc.StandardInput.WriteLine(20);
            }
    
    

     

    For more FAQ about Visual C# General, please see Visual C# General FAQ

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

    Tuesday, April 7, 2009 2:07 PM