none
Hiding the command window when redirecting the standard output RRS feed

  • Question

  • Hello,

    I have the following code:

    ProcessStartInfo serverInfo = new ProcessStartInfo("Myserver");
                serverInfo.FileName = "C:/something.exe";
                serverInfo.Arguments = "";
                serverInfo.WindowStyle = ProcessWindowStyle.Hidden;
                serverInfo.UseShellExecute = false;
                serverInfo.RedirectStandardError = true;
                serverInfo.RedirectStandardOutput = true;
    
                //Process object
                EDMserver = new Process();
                EDMserver.StartInfo = serverInfo;
                EDMserver.OutputDataReceived += new DataReceivedEventHandler(OnDataReceived);
                EDMserver.Start();
                EDMserver.BeginOutputReadLine();
                EDMserver.WaitForExit();

    The "something.exe" is an old C app that prints out its status in the command window.  I want to hide the command window and redirect the IO to a file. So my question is how do I redirect the IO while the command window is hidden since the code above does not work.

    cheers,

    Ehsan


    Tuesday, May 22, 2012 11:44 AM

Answers

  •         Process proc;
            private void btnStartConsoleApplication1_Click(object sender, EventArgs e)
            {
                ProcessStartInfo procinfo = new ProcessStartInfo("ConsoleApplication1.exe");
                procinfo.CreateNoWindow = true;
                procinfo.WindowStyle = ProcessWindowStyle.Hidden;
                procinfo.RedirectStandardError = true;
                procinfo.RedirectStandardInput = true;
                procinfo.RedirectStandardOutput = true;
                procinfo.UseShellExecute = false;
    
                proc = new Process();
                proc.StartInfo = procinfo;
                proc.OutputDataReceived += new DataReceivedEventHandler(proc_OutputDataReceived);
                proc.Start();
                if (procinfo.RedirectStandardOutput)
                    proc.BeginOutputReadLine();
            }
    Please also set the ProcessStartInfo.CreateNoWindow property to true.

    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by ehsansad Wednesday, May 23, 2012 7:08 AM
    Wednesday, May 23, 2012 4:48 AM
    Moderator

All replies

  • Take a look at the example for Process.OutputDataRecieved.
    Tuesday, May 22, 2012 11:47 AM
  • The example does not try to hide the command window. I want to hide the command window and also redirect the output to a file. The problem with my example is that this line

    serverInfo.WindowStyle = ProcessWindowStyle.Hidden;

    has no effect after i added these lines

      serverInfo.UseShellExecute = false;
                serverInfo.RedirectStandardError = true;
                serverInfo.RedirectStandardOutput = true;
    

    The command window comes up and correctly does not show anything, but this was not my intention. I wanted the command windows to be hidden.

    any ideas?

    -ehsan 

    Tuesday, May 22, 2012 1:10 PM
  • Is it showing the command window for the newly created process, or just the window for the app that creates the new process, because hiding that window is another matter entirely.

    Does the process read anything from standard input?  Could it be waiting on input before it can continue?  Does it assume that it's working directory is the same as it's location?  I notice you haven't set that.  Have you debugged the app and set a breakpoint in the OnDataReceived event handler to see if it's even being fired at all?

    Tuesday, May 22, 2012 2:44 PM
  • it the process window. I have tried it without the redirecting and all work correctly, the window is hidden and the app is running. When I added the redirect code 

     serverInfo.UseShellExecute = false;
                serverInfo.RedirectStandardError = true;
                serverInfo.RedirectStandardOutput = true;

    The command window appeared. and no I haven't tried to debug it and I'll do it first thing in the morning.

    Tuesday, May 22, 2012 4:45 PM
  •         Process proc;
            private void btnStartConsoleApplication1_Click(object sender, EventArgs e)
            {
                ProcessStartInfo procinfo = new ProcessStartInfo("ConsoleApplication1.exe");
                procinfo.CreateNoWindow = true;
                procinfo.WindowStyle = ProcessWindowStyle.Hidden;
                procinfo.RedirectStandardError = true;
                procinfo.RedirectStandardInput = true;
                procinfo.RedirectStandardOutput = true;
                procinfo.UseShellExecute = false;
    
                proc = new Process();
                proc.StartInfo = procinfo;
                proc.OutputDataReceived += new DataReceivedEventHandler(proc_OutputDataReceived);
                proc.Start();
                if (procinfo.RedirectStandardOutput)
                    proc.BeginOutputReadLine();
            }
    Please also set the ProcessStartInfo.CreateNoWindow property to true.

    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by ehsansad Wednesday, May 23, 2012 7:08 AM
    Wednesday, May 23, 2012 4:48 AM
    Moderator