使用System.Diagnostics.Process 類別在遠端電腦上建立進程時,可能需要阻止控制台視窗的顯示。但是,設定 CreateNoWindow 和 WindowStyle 屬性可能不會總是足夠。
一個可能的解決方案源自於 UseShellExecute 屬性和上述設定之間的交互作用。根據 MSDN 文件:
如果 UseShellExecute 屬性為 true 或 UserName 和 Password 屬性不為空,則忽略 CreateNoWindow 屬性值並建立新視窗。
因此,請確保UseShellExecute 設定為 false 對於有效的視窗抑制至關重要。以下程式碼範例示範了這種方法:
ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = fullPath; startInfo.Arguments = args; startInfo.RedirectStandardOutput = true; startInfo.RedirectStandardError = true; startInfo.UseShellExecute = false; startInfo.CreateNoWindow = true; Process processTemp = new Process(); processTemp.StartInfo = startInfo; processTemp.EnableRaisingEvents = true; try { processTemp.Start(); } catch (Exception e) { throw; }
以上是C#啟動進程時如何隱藏控制台視窗?的詳細內容。更多資訊請關注PHP中文網其他相關文章!