128

I am trying to create process on a remote machine using using System.Diagnostics.Process class. I am able to create a process. But the problem is, creating a service is take a long time and console window is displayed. Another annoying thing is the console window is displayed on top of my windows form and i cant do any other operations on that form. I have set all properties like CreateNoWindow = true,

proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden

but still it shows the console window. even i have redirected output and errors to seperate stream but no luck.

Is there any other way to hide the Console window? Please help me out .

Here is the part of my code i used to execute sc command.

Process proc = new Process();
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.StartInfo.FileName = "sc";
proc.StartInfo.Arguments = string.Format(@"\\SYS25 create MySvc binPath= C:\mysvc.exe");
proc.StartInfo.RedirectStandardError = false;
proc.StartInfo.RedirectStandardOutput = false;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
proc.WaitForExit();
Prasad
  • 1,847
  • 5
  • 21
  • 26
  • Have you tried setting `proc.StartInfo.RedirectedStandardOutput = true`? I've gotten this to work with a Windows Application. – poy Dec 06 '12 at 21:09
  • any final solution with full source code sample working about it ? – Kiquenet Jul 10 '13 at 08:51
  • In a Windows *console* app, simply setting UseShellExecute - w/o redirection or anything else - worked for me. See [here](https://stackoverflow.com/a/47781912/63209). – Paul Dec 12 '17 at 21:49
  • As far as the console window opening and locking out the form, you should run the process as a Task or on a new Thread – Ryan Dooley Feb 01 '18 at 16:23
  • 2
    Why are you setting `UseShellExecute` and `CreateNoWindow` twice? – Mike Lowery Feb 28 '19 at 04:31

4 Answers4

158

I had a similar issue when attempting to start a process without showing the console window. I tested with several different combinations of property values until I found one that exhibited the behavior I wanted.

Here is a page detailing why the UseShellExecute property must be set to false.
http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.createnowindow.aspx

Under Remarks section on page:

If the UseShellExecute property is true or the UserName and Password properties are not null, the CreateNoWindow property value is ignored and a new window is created.

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = fullPath;
startInfo.Arguments = args;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;

Process processTemp = new Process();
processTemp.StartInfo = startInfo;
processTemp.EnableRaisingEvents = true;
try
{
    processTemp.Start();
}
catch (Exception e)
{
    throw;
}
Fedor
  • 1,548
  • 3
  • 28
  • 38
John Bartels
  • 2,583
  • 3
  • 19
  • 26
  • 2
    Care to add some additional details? The **why** this works rather than the **how**. – aqua May 24 '13 at 00:26
  • 2
    Is it because the standard output is forcing the window to exist? – Denise Skidmore May 31 '13 at 14:14
  • 1
    And apparently you can't redirect standard output without setting `startInfo.UseShellExecute = false;` – Denise Skidmore May 31 '13 at 14:19
  • 1
    I'm curious why, in this other answer they used UseShellExecute = true? http://stackoverflow.com/questions/2317767/c-process-start-hide – Denise Skidmore May 31 '13 at 17:20
  • @rene Ok, I will. Added it only because syntax highlighting was't working. – Fedor Mar 25 '14 at 13:37
  • 1
    @Fyodor refer to [this](http://meta.stackexchange.com/questions/184108/what-is-syntax-highlighting-and-how-does-it-work) but if you have reproduceable case were it doesn't work please post a bug report on meta. – rene Mar 25 '14 at 13:45
  • @rene thank you, I think it was just extra long lag before highlighting applied, I'll be more patient next time :) – Fedor Mar 25 '14 at 13:48
  • 22
    I'll never understand why people catch an exception they're just throwing again anyway. – Blindy Nov 09 '15 at 20:39
  • 3
    I placed the try catch in this code mostly for brevity (to show that this portion of code could possibly throw an exception, and something should be done with it). In the past, the reason I throw the exception again is due to the fact that this code (in my application) is executed using codedom, reflection, and csharpcodeprovider. Re-throwing the exception allows it to bubble up to an upper layer of code without adding on an additional exception in the stack trace (if I remember correctly). I'm not entirely sure it's necessary, but it is simply the way I created that application. – John Bartels Nov 10 '15 at 13:11
  • 1
    Sometimes I like breaking on explicit exceptions when debugging, so catching then throwing can help with that. – CamHart Apr 07 '20 at 03:43
  • `startInfo.CreateNoWindow = true;` was the key to hide the console window in my case. – RBT Jun 29 '21 at 07:43
63

I've had bad luck with this answer, with the process (Wix light.exe) essentially going out to lunch and not coming home in time for dinner. However, the following worked well for me:

Process p = new Process();
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
// etc, then start process
Fedor
  • 1,548
  • 3
  • 28
  • 38
John Lockwood
  • 3,787
  • 29
  • 27
27

This should work, try;


Add a System Reference.

using System.Diagnostics;

Then use this code to run your command in a hiden CMD Window.

Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
cmd.StartInfo.Arguments = "Enter your command here";
cmd.Start();
Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
15

This doesn't show the window:

Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
cmd.StartInfo.CreateNoWindow = true;

...
cmd.Start();
Leonardo Rignanese
  • 865
  • 11
  • 22