25

I start a Console Application via ProcessStartInfo and process.Start(). I want to hide the black window. Here's my code:

string output = "";
//Setup the Process with the ProcessStartInfo class
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "C:\\WINNT\\system32\\cmd.exe";
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;

//Start the process
Process proc = Process.Start(startInfo);
Cyclonecode
  • 29,115
  • 11
  • 72
  • 93
Thu marlo
  • 543
  • 2
  • 6
  • 10
  • any final solution with full source code sample working about it ? CreateNoWindow = true ? – Kiquenet Jul 10 '13 at 08:50
  • 1
    possible duplicate of [Hide console window from Process.Start C#](http://stackoverflow.com/questions/5377423/hide-console-window-from-process-start-c-sharp) – sirdank May 06 '15 at 18:58

4 Answers4

40

The final answer is

 ProcessStartInfo psi = new ProcessStartInfo();
 psi.FileName = ....
 psi.RedirectStandardInput = true;
 psi.RedirectStandardOutput = false;
 psi.Arguments =...
 psi.UseShellExecute = false;

psi.CreateNoWindow = true; // <- key line

Community
  • 1
  • 1
Oumdaa
  • 677
  • 6
  • 14
  • 4
    CreateNoWindow did the trick for me where WindowStyle had no effect. I'm redirecting standard output anyway and can now avoid the empty window. – Timo Jan 12 '15 at 08:48
38

Try This:

startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
Jonathan
  • 11,809
  • 5
  • 57
  • 91
  • 4
    That will do the trick. Only add that it has to be set before calling `Process.Start(startInfo)`. – Cipi Jul 28 '11 at 09:54
  • 1
    doesnt work, but... you get an error it simply show the black window? I need more info. :D – Jonathan Jul 28 '11 at 11:10
  • I tried your code and I'm getting and error related with pipes, not with your actual question. If I replace FileName with a .bat file, It run without problems and no black windows are shown. – Jonathan Jul 28 '11 at 11:21
9

Try

startInfo.WindowStyle = ProcessWindowStyle.Hidden;
Morten Anderson
  • 2,301
  • 15
  • 20
8
Process p = new Process();
....
p.StartInfo.CreateNoWindow = true;
p.Start();
Ads
  • 2,084
  • 2
  • 24
  • 34