How to hide cmd window while running a batch file?

batch-file, c#, process

Solution

If proc.StartInfo.UseShellExecute is false, then you are launching the process and can use:

proc.StartInfo.CreateNoWindow = true;

If proc.StartInfo.UseShellExecute is true, then the OS is launching the process and you have to provide a "hint" to the process via:

proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

However the called application may ignore this latter request.

If using UseShellExecute = false, you might want to consider redirecting standard output/error, to capture any logging produced:

proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.OutputDataReceived += new DataReceivedEventHandler(ProcessOutputHandler);
proc.StartInfo.RedirectStandardError = true;
proc.ErrorDataReceived += new DataReceivedEventHandler(ProcessOutputHandler);

And have a function like

private void ProcessOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
   if (!String.IsNullOrEmpty(outLine.Data)) // use the output outLine.Data somehow;
}

There's a good page covering `CreateNoWindow` this on an MSDN blog.

There is also a bug in Windows which may throw a dialog and defeat `CreateNoWindow` if you are passing a username/password. For details

http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=98476 http://support.microsoft.com/?kbid=818858

Problem

How to hide cmd window while running a batch file? I use the following code to run batch file ``` process = new Process(); process.StartInfo.FileName = batchFilePath; process.Start(); ```

Original source