Process.Start 報錯 System.Diagnostics.Process.StartWithShellExecuteEx
Process.Start 為什么會引發“系統找不到指定的文件”異常
Process.Start 報錯 找不到路徑 ,System.ComponentModel.Win32Exception:“系統找不到指定的文件。
問題1、
在WinForm中可能是權限問題,設置文件夾和文件權限即可,也可能是NET版本太低了,只要把項目版本從net2.0? 換成4.0以及以上,同時?解決方案平臺設置位 AnyCPU?即可
//報錯找不到路徑 ,System.ComponentModel.Win32Exception:“系統找不到指定的文件。” ----只要把net2.0 換成4.0以及以上,同時 解決方案平臺設置位 AnyCPU 即可
//Process.Start(@"F:\osk.exe");
var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "osk.exe");
Process.Start(path);var p = new Process();p.StartInfo = new ProcessStartInfo(path){WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.System),UseShellExecute = true,Verb = "runas" //管理員權限};p.Start();
問題2、
在NET6 中可能是NET Framework 版本的最后執行的是StartWithShellExecuteEx
而不是StartWithCreateProcess方法
造成這樣的原因,是因為UseShellExecute
在 .NET 6 上默認為 false:
public bool UseShellExecute { get; set; }
而在 .NET Framework 上默認為 true:
//
// 摘要:
// Gets or sets a value indicating whether to use the operating system shell to
// start the process.
//
// 返回結果:
// true if the shell should be used when starting the process; false if the process
// should be created directly from the executable file. The default is true.
[DefaultValue(true)]
[MonitoringDescription("ProcessUseShellExecute")]
[NotifyParentProperty(true)]
public bool UseShellExecute { get; set; }
問題3、
如果想使用參數的話,需要在?Arguments上設置參數
var p = new Process();p.StartInfo = new ProcessStartInfo(path){WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.System),UseShellExecute = true,Verb = "runas",Arguments="參數"};p.Start();