.NETCore用Process.Start打开网址出现异常
在.NETFramework中我们用默认浏览器打开一个网址,可以这么写
C# 全选
System.Diagnostics.Process.Start(url);
这种写法在.NET Core中会报错,异常如下:
System.ComponentModel.Win32Exception:“An error occurred trying to start process 'https://www.jocsoft.cn' with working directory 'D:\Projects\TextToSpeech\TextToSpeech\bin\Debug\net7.0-windows'. 系统找不到指定的文件。”
解决方案
.NETCore中我们可以用以下的方式打开网址
C# 全选
//System.Diagnostics.Process.Start(url);
var psi = new ProcessStartInfo
{
FileName = url,
UseShellExecute = true
};
Process.Start(psi);
原因是因为 UseShellExecute 属性在.NET Core中默认为 false,要打开一个网页链接应该将其设为 true,因为url并不是一个可执行文件。
版权声明:本文为YES开发框架网发布内容,转载请附上原文出处连接
post 张国生