none
How to run a .EXE file from C# and obtain(pipe) the results from DOS windows back into the C#

    Question

  • Hello all,

     

    I have a vbs script which executes the following line passing a bunch of arguments:

     

    set objExec = objShell.Exec(path & " " & version & " " & webservice & " " & strLine)

     

    The command line looks like this:

    C:>\Tools\UpdateDevice.exe "1.1.3945.1" "http://localhost/bss/PrincipalManagement.asmx" "11111"

     

    I manage to execute that from C# in the following way:


                const String strToolPath = "c:\\Tools\\UpdateDevice.exe";
                const String strWebService = "http://localhost/bss/PrincipalManagement.asmx";

                string strArguments = " " + strVersion + " " + strWebService + " " + strGuid;

               
                try
                {
                    System.Diagnostics.Process.Start(strToolPath, strArguments);
                }
                catch ( Exception ex )
                {
                    System.Diagnostics.EventLog.WriteEntry("STBUpdate", ex.Message, EventLogEntryType.Error, 1111);
                }
                finally
                {

                }

     

    HOWEVER, the executable itself is managing the exception and it throws the error in the DOS window then it returns to the C# program and I only see the error in the screen.

     

    How do I capture that error so that I can show the error inside my C# code?

     

    Thank you

     

    JCDS

    Wednesday, April 11, 2007 8:04 PM

Answers

  • You'll have to redirect StandardOutput (and/or StandardError)  see my post on this other thread for an example.
    Wednesday, April 11, 2007 8:23 PM
  • In the content of that program I'm sending commands to the plink executable  through the redirected StandardInput, in this case I was running the "ls" command in the linux shell I had connected to.  In the situation you're referring to you don't seem to have to worry about the StandardInput section.
    Wednesday, April 11, 2007 10:04 PM

All replies

  • You'll have to redirect StandardOutput (and/or StandardError)  see my post on this other thread for an example.
    Wednesday, April 11, 2007 8:23 PM
  • Michael,

     

    That pointed me to the right direction. Thank you so much.
    The only thing I am curious about is the lines below:


    p.Start();
    StreamReader reader = p.StandardOutput;
    StreamWriter writer = p.StandardInput;
    writer.AutoFlush = true;
    writer.Write("ls\n");    <==== ? what do these do?
    writer.Write("exit\n"); <==== ? what do these do?
    s = reader.ReadToEnd();
    Console.WriteLine(s);
    Console.ReadLine();

     

    Thank you

    JCDS

    Wednesday, April 11, 2007 9:47 PM
  • In the content of that program I'm sending commands to the plink executable  through the redirected StandardInput, in this case I was running the "ls" command in the linux shell I had connected to.  In the situation you're referring to you don't seem to have to worry about the StandardInput section.
    Wednesday, April 11, 2007 10:04 PM
  • That is what I thought :-))))

     

    Thank you!

    Wednesday, April 11, 2007 10:07 PM