.NET - WindowStyle = hidden与CreateNoWindow = true?

Gab*_*bor 62 .net c# process processstartinfo

当我开始一个新的过程时,如果我使用它会有什么不同

WindowStyle = Hidden
Run Code Online (Sandbox Code Playgroud)

或者

CreateNoWindow = true
Run Code Online (Sandbox Code Playgroud)

ProcessStartInfo班级的财产?

Liz*_*Liz 76

正如汉斯所说,WindowStyle是一个传递给流程的推荐,应用程序可以选择忽略它.

CreateNoWindow控制控制台如何为子进程工作,但它不能单独工作.

CreateNoWindow与UseShellExecute一起使用,如下所示:

在没有任何窗口的情况下运行该过程

ProcessStartInfo info = new ProcessStartInfo(fileName, arg); 
info.CreateNoWindow = true; 
info.UseShellExecute = false;
Process processChild = Process.Start(info); 
Run Code Online (Sandbox Code Playgroud)

在其自己的窗口中运行子进程(新控制台)

ProcessStartInfo info = new ProcessStartInfo(fileName, arg); 
info.UseShellExecute = true; // which is the default value.
Process processChild = Process.Start(info); // separate window
Run Code Online (Sandbox Code Playgroud)

在父级控制台窗口中运行子进程

ProcessStartInfo info = new ProcessStartInfo(fileName, arg); 
info.UseShellExecute = false; // causes consoles to share window 
Process processChild = Process.Start(info); 
Run Code Online (Sandbox Code Playgroud)

  • 值得一提的是,我学到了很多困难:如果你创建一个Process然后修改它的StartInfo,你会得到一个不同于你创建ProcessStartInfo然后使用Process.Start()的行为.具体来说,前者似乎不尊重CreateNoWindow. (18认同)
  • @AriRoth你能给出一个代码示例吗?您必须先创建一个进程,然后才能修改其StartInfo.人们总是会说process1.StartInfo =某事.因此,您必须首先创建流程,然后再分配给process1.StartInfo的ProcessStartInfo实例,否则无法执行任何其他操作. (2认同)

Han*_*ant 17

CreateNoWindow仅适用于控制台模式应用程序,它不会创建控制台窗口.

WindowStyle仅适用于本机Windows GUI应用程序.它是一个提示传递给这样一个程序的WinMain()入口点.第四个参数,nCmdShow,告诉它如何显示其主窗口.这是在桌面快捷方式中显示为"运行"设置的相同提示.请注意,"隐藏"不是那里的选项,很少有正确设计的Windows程序尊重该请求.由于这会扼杀用户,他无法再激活程序,只能使用任务管理器将其终止.


SwD*_*n81 13

使用反射器,它看起来像WindowStyle如果使用UseShellExecute设置,否则使用CreateNoWindow.

在MSDN的示例中,您可以看到它们如何设置它:

// Using CreateNoWindow requires UseShellExecute to be false
myProcess.StartInfo.UseShellExecute = false;
// You can start any process, HelloWorld is a do-nothing example.
myProcess.StartInfo.FileName = "C:\\HelloWorld.exe";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
Run Code Online (Sandbox Code Playgroud)

在另一个例子中,它正好在下面,因为UseShellExecute默认为true

// UseShellExecute defaults to true, so use the WindowStyle
ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe");
startInfo.WindowStyle = ProcessWindowStyle.Minimized;
Run Code Online (Sandbox Code Playgroud)