Hiding the process window, why isn't it working?
Asked Answered
A

2

9

I have tried several things now to hide the window of a new process (in this case it's just notepad.exe for testing), but it just won't work regardless of what I try.

I have read many posts now all saying the same so why isn't it working for me?

I have a console app that is supposed to launch other processes without showing their windows.

I have tried to make my console app launch notepad.exe without a window, but it just won't work.

ProcessStartInfo info = new ProcessStartInfo("path to notepad.exe");

info.RedirectStandardOutput = true;
info.RedirectStandardError = true;                                
info.CreateNoWindow = true;
info.UseShellExecute = false;                                

Process proc = Process.Start(info);

I have also tried using various setting for info.WindowStyle and I have tried to configure my console app to be a Windows application, but it doesn't really matter what I do, the child process always opens a window.

Is this not allowed from a console app or what is the problem here - can anyone shed some light on this maybe?

I'm using .NET 4.0 on Windows 7 x64

Aho answered 23/4, 2014 at 13:58 Comment(2)
Did you try info.WindowStyle = ProcessWindowStyle.Hidden;?Synchro
Yes I did, but apparently it doesn't give the desired effect when the other properties are set as well. However it seems Grant has found the solution.Aho
S
22

In my experience, the following works whenever I fire up "cmd.exe".

info.CreateNoWindow = true;
info.UseShellExecute = false;                                

It doesn't seem to work with "notepad.exe". It fails with other apps too, like "excel.exe" and "winword.exe".

This works, however:

ProcessStartInfo info = new ProcessStartInfo("notepad.exe");

info.WindowStyle = ProcessWindowStyle.Hidden;

Process proc = Process.Start(info);

From MSDN:

A window can be either visible or hidden. The system displays a hidden window by not drawing it. If a window is hidden, it is effectively disabled. A hidden window can process messages from the system or from other windows, but it cannot process input from the user or display output. Frequently, an application may keep a new window hidden while it customizes the window's appearance, and then make the window style Normal. To use ProcessWindowStyle.Hidden, the ProcessStartInfo.UseShellExecute property must be false.

When I tested it, I didn't have to set UseShellExecute = false.

Scutch answered 23/4, 2014 at 14:1 Comment(3)
This works! when setting no properties but WindowStyle, then it seems to work for some reason. Thanks Grant :)Aho
The MSDN documentation has been corrected to say "To use Hidden, the UseShellExecute property must be true." UseShellExecute defaults to true.Dealings
@TimSparkles Nice update from MS. Thanks for the infoForcer
T
0

info.CreateNoWindow = true;

above line will prevent to display in c#
I have use below code to run exe in background and also read output from the exe:

string ExeName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Curl.exe");
ProcessStartInfo Info = new ProcessStartInfo();
Info.UseShellExecute = false;
Info.FileName = ExeName;
Info.CreateNoWindow = true;
Info.Arguments = Arguments;
Info.RedirectStandardOutput = true;
Info.WindowStyle = ProcessWindowStyle.Hidden;
Process P = Process.Start(Info);
string OutPut = P.StandardOutput.ReadToEnd();
return OutPut;

Thimbleweed answered 26/8, 2021 at 10:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.