22

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();
Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
Ahmed Atia
  • 17,848
  • 25
  • 91
  • 133

5 Answers5

44

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

Joel Goodwin
  • 5,026
  • 27
  • 30
  • Sometimes works, others doesn't. Does this depend on bat file commands? – Ahmed Atia Jul 08 '09 at 08:17
  • Ahmed, I've made the answer more comprehensive and the two different options you have for hiding the window. Also, depending on the application, I think you can still have it break out and make some windows despite your better efforts. – Joel Goodwin Jul 08 '09 at 09:33
8

According to the Process properties, you do have a:

Property: CreateNoWindow
Notes: Allows you to run a command line program silently. It does not flash a console window.

and:

Property: WindowStyle
Notes: Use this to set windows as hidden. The author has used ProcessWindowStyle.Hidden often.

As an example!

static void LaunchCommandLineApp()
{
    // For the example
    const string ex1 = "C:\\";
    const string ex2 = "C:\\Dir";

    // Use ProcessStartInfo class
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.CreateNoWindow = false;
    startInfo.UseShellExecute = false;
    startInfo.FileName = "dcm2jpg.exe";
    startInfo.WindowStyle = ProcessWindowStyle.Hidden;
    startInfo.Arguments = "-f j -o \"" + ex1 + "\" -z 1.0 -s y " + ex2;

    try
    {
        // Start the process with the info we specified.
        // Call WaitForExit and then the using statement will close.
        using (Process exeProcess = Process.Start(startInfo))
        {
            exeProcess.WaitForExit();
        }
    }
    catch
    {
        // Log error.
    }
}
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
5

Use: process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

1

This is what worked for me, When you redirect all of the input and output, and set the window hidden it should work

            Process p = new Process();
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.CreateNoWindow = true;
Mnyikka
  • 1,223
  • 17
  • 12
0

try with this and this where the c# code is embedded into the batch files:

@echo off

echo self minimizing
call getCmdPid.bat
call windowMode.bat -pid %errorlevel% -mode minimized

echo --other commands--
pause

Though it might be not so easy to unhide the window.

npocmaka
  • 55,367
  • 18
  • 148
  • 187