在 .NET Framework 中,如果想使用默认浏览器打开一个 URL 可以使用以下代码:
Process.Start("https://hovertree.com");
但该代码在 .NET 6.0 或者.NET 7.0 中执行会报错:
System.ComponentModel.Win32Exception
HResult=0x80004005
Message=
An error occurred trying to start process ‘https://hovertree.com’ with working directory ‘xxxx’.
系统找不到指定的文件。
Source=System.Diagnostics.Process
StackTrace:
在 System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
在 System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
出现该错误的原因是 .NET CORE 改变了 ProcessStartInfo 类的默认值。在默认情况下 UseShellExecute 的取值由 .NET Framework 中的 True 变为了 False 。
所以,要解决该报错只需将 UseShellExecute 设置为 True 即可:
Process.Start(new ProcessStartInfo("https://hovertree.net") { UseShellExecute = true });