Lufthansa
Lufthansa

Reputation: 145

C# hide, background, processinfo

I have this code:

ProcessStartInfo PSI = new ProcessStartInfo("cmd.exe");
PSI.CreateNoWindow = true;
PSI.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
PSI.RedirectStandardInput = true;
PSI.RedirectStandardOutput = true;
PSI.RedirectStandardError = true;
PSI.UseShellExecute = true;
Process p = Process.Start(PSI);

problem is, when I build it, the command prompt still appears. How can I hide it?Thanks!

Upvotes: 2

Views: 581

Answers (2)

Nahydrin
Nahydrin

Reputation: 13517

After copy pasting your code, there's an exception that you probably aren't noticing. To redirect the IO streams, the UseShellExecute property must be set to false.

Also, PSI.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; is not required. Here is your working code:

ProcessStartInfo PSI = new ProcessStartInfo("cmd.exe");
PSI.CreateNoWindow = true;
//PSI.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
PSI.RedirectStandardInput = true;
PSI.RedirectStandardOutput = true;
PSI.RedirectStandardError = true;
PSI.UseShellExecute = false;
Process p = Process.Start(PSI);

Upvotes: 0

Justin M. Keyes
Justin M. Keyes

Reputation: 6964

In Visual Studio, change the output type under Application in the project properties to Windows Application.

Project Properties > Application > Output Type: "Windows Application"

Also try:

PSI.UseShellExecute = false;

Upvotes: 1

Related Questions