1

I have this:

        var startInfo = new ProcessStartInfo
                            {
                                FileName = _pathToExe,
                                RedirectStandardOutput = true,
                                RedirectStandardError = true,
                                RedirectStandardInput = true,
                                UseShellExecute = false,
                                WorkingDirectory = FilepathHelper.GetFolderFromFullPath(_pathToExe),
                                CreateNoWindow = true,
                                WindowStyle = ProcessWindowStyle.Hidden
                            };

        try
        {
            using (_proc = Process.Start(startInfo))
            {
                _proc.EnableRaisingEvents = true;
                _proc.ErrorDataReceived += proc_DataReceived;
                _proc.OutputDataReceived += proc_DataReceived;
                _proc.BeginErrorReadLine();
                _proc.BeginOutputReadLine();

                var myStreamWriter = _proc.StandardInput;

                var allArgs = "";
                foreach (var arg in _args)
                    allArgs += arg + Environment.NewLine;

                myStreamWriter.Write(allArgs);
                _proc.WaitForExit();
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }

And I am executing an *.exe that someone else wrote. For this particular *.exe - even though you can clearly see above that I have set ProcessWindowStyle.Hidden, I still see a black window appear with the words "Press any key to exit.". This *.exe - if I run from the command line (instead of calling it from my C# code) produces a tremendous amount of console output text. I do not see this output text when I run my C# code, which is what I want and also means the redirection is working.

I checked and the process is finished - it's as if the command window itself is adding this extra (undesirable) step.

Has anyone encountered this before and if so how can I get rid of this?

Yahia
  • 69,653
  • 9
  • 115
  • 144
sapbucket
  • 6,795
  • 15
  • 57
  • 94

2 Answers2

0

From Document:

To use System.Diagnostics.ProcessWindowStyle.Hidden, the system.Diagnostics.ProcessStartInfo.UseShellExecute property must be true.

Shannon
  • 54
  • 1
  • 11
0

It seems that the program you are starting is calling system("PAUSE") at the end thus spawning a new process which prints the "Press Any Key to Continue..." message and waits for user input. I cannot reproduce the exact situation of yours but you can try this.

        var startInfo = new ProcessStartInfo
        {
            FileName = "cmd.exe", 
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            RedirectStandardInput = true,
            UseShellExecute = false,
            CreateNoWindow = true,
        };

        try
        {
            Process _proc;
            using (_proc = Process.Start(startInfo))
            {
                _proc.EnableRaisingEvents = true;
                _proc.BeginErrorReadLine();
                _proc.BeginOutputReadLine();

                var myStreamWriter = _proc.StandardInput;

                myStreamWriter.WriteLine("D:\\your.exe"); //write your.exe to cmd and press enter :) 
                _proc.WaitForExit();

            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
Mihail Shishkov
  • 14,129
  • 7
  • 48
  • 59
  • Sorry, this isn't the solution. You fundamentally changed my question by switching FileName to cmd.exe. – sapbucket Feb 02 '12 at 00:02
  • You can start cmd.exe and then run your.exe within it with the help of myStreamWriter. This is the same as running your.exe directly but in the context of cmd.exe's console. You can then wait for the "Press any key to continue..." string to be read indicating the process has done its job and than close it. – Mihail Shishkov Feb 02 '12 at 08:20