我正在使用 System.Diagnostics.Process 类启动一些可执行文件,如下所示:
C#代码:
Process newProcess = new Process();
newProcess.StartInfo.FileName = path;
newProcess.StartInfo.Arguments = parameter;
newProcess.StartInfo.WorkingDirectory = workingDirectory;
newProcess.StartInfo.UseShellExecute = false;
newProcess.StartInfo.WindowStyle = windowStyle;
newProcess.StartInfo.RedirectStandardError = true;
newProcess.StartInfo.RedirectStandardOutput = true;
newProcess.Start();
一切正常,除了
WindowStyle
未按我想要的方式设置。它保持默认值。如果 UseShellExecute
设置为 true,则 WindowStyle
会按照我想要的方式更改,但我不能再使用 RedirectStandardError
和 RedirectStandardOutput
,因为它需要 UseShellExecute
设置为 false。我一直在寻找答案,但没有找到任何可以期待的东西,即您不能将 UseShellExecute
设置为 false 并编辑 WindowStyle
。这是真的吗?
我可以使用什么方法来获取标准和错误输出,并且仍然能够更改
WindowStyle
?
有同样的问题:RedirectStandardOutput 要求 UseShellExecute 为 false,而 WindowStyle 仅在 UseShellExecute 为 true 时才有效。
以下内容对我有用在开始该过程之后:
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
static int SW_HIDE = 0;
// Wait a little for the process to start otherwise handle is 0
IntPtr mainHandle = Server.MainWindowHandle;
bool result = ShowWindow(mainHandle, SW_HIDE);
与 StartInfo.WindowStyle 相比有两个不便之处: