[英]When do we need to set ProcessStartInfo.UseShellExecute to True?
[英]How to decide whether or not to set ProcessStartInfo.UseShellExecute Property to true or false?
我正在從我的 C# windows 窗體應用程序調用/啟動 .exe 應用程序和 .bat 批處理文件(我也在傳遞參數)。 如何確定是否需要將 ProcessStartInfo.UseShellExecute 屬性設置為 true 或 false? 什么情況下真比假好,反之亦然?
我正在使用 ProcessStartInfo 類來設置所有進程信息。 然后我使用 System.Diagnostics.Process.Start(ProcessStartInfo) 方法根據我的 ProcessStartInfo 啟動進程。
using System.Diagnostics;
ProcessStartInfo processInfo = new ProcessStartInfo();
processInfo.FileName = "[something]";
processInfo.Arguments = "[parameter1] [parameter2] [parameter3]";
Process.Start(processInfo);
我希望這些流程能夠啟動並正常工作。 如果我需要添加更多信息/澄清一些事情,請告訴我,這是我的第一個問題。
您可以在 .NET 源中看到:
內部函數StartWithShellExecuteEx => 調用ShellExecuteEx
內部函數StartWithCreateProcess => 調用CreateProcessWithLogonW或CreateProcessW
當您想要啟動與擴展相關的可執行文件時,通常使用 true。 喜歡 :
using (Process p = new Process())
{
string sImage = @"e:\test.jpg";
p.StartInfo.FileName = sImage;
p.StartInfo.Verb = "open";
p.StartInfo.UseShellExecute = true;
p.Start();
}
如果我設置為 false,它將失敗,因為它會嘗試使用“e:\\test.jpg”作為命令行調用 CreateProcessW
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.