我现在已经尝试了几种方法来隐藏新进程的窗口(在这种情况下,它只是用于测试的 notepad.exe),但无论我尝试什么,它都不会起作用。
我现在已经阅读了很多帖子都说同样的话,为什么它对我不起作用?
我有一个控制台应用程序,它应该在不显示其窗口的情况下启动其他进程。
我曾尝试让我的控制台应用程序在没有窗口的情况下启动 notepad.exe,但它根本行不通。
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);
我也尝试过对 info.WindowStyle 使用各种设置,并且我尝试将我的控制台应用程序配置为 Windows 应用程序,但我做什么并不重要,子进程总是打开一个窗口。
这是控制台应用程序不允许的还是这里的问题是什么 - 任何人都可以对此有所了解吗?
我在 Windows 7 x64 上使用 .NET 4.0
最佳答案
根据我的经验,每当我启动“cmd.exe”时,以下都会起作用。
info.CreateNoWindow = true;
info.UseShellExecute = false;
它似乎不适用于“notepad.exe”。它也无法与其他应用程序一起使用,例如“excel.exe”和“winword.exe”。
然而,这有效:
ProcessStartInfo info = new ProcessStartInfo("notepad.exe");
info.WindowStyle = ProcessWindowStyle.Hidden;
Process proc = Process.Start(info);
来自 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.
当我测试它时,我不必设置 UseShellExecute = false
。
关于c# - 隐藏进程窗口,为什么不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23246613/