views:

5467

answers:

4

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();
+4  A: 

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

ThatGuy
Sometimes works, others doesn't. Does this depend on bat file commands?
Ahmed
+5  A: 

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
Sometimes works, others doesn't. Does this depend on bat file commands?
Ahmed
+12  A: 

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
Sometimes works, others doesn't. Does this depend on bat file commands?
Ahmed
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
A: 

im too stupid to understand this. StartInfo.WindowStyle = ProcessWindowStyle.Hidden were i have to write this crap to run bat file hidden?

jkdshf
This question assumes that you are starting a batch file from inside a C# program. Setting something like ProcessWindowStyle is something that your program has to do.
JeffH