哪个网站做logo赚钱,医院 网站源码,用jsp做视频网站,网站备案必须在公司注册地前言偶然发现#xff0c;如果想用如下代码在 .NET 6 中打开指定 URL#xff1a;Process.Start(https://baidu.com);会引发异常#xff1a;而同样的代码在 .NET Framework 中是可以正常执行的。难道#xff0c;.NET 6 下的实现逻辑不一样#xff1f;深入探究通… 前言偶然发现如果想用如下代码在 .NET 6 中打开指定 URLProcess.Start(https://baidu.com);会引发异常而同样的代码在 .NET Framework 中是可以正常执行的。难道.NET 6 下的实现逻辑不一样深入探究通过调用堆栈我们发现最后调用的是StartWithCreateProcess方法对应的 .NET 6 源代码如下private bool StartCore(ProcessStartInfo startInfo)
{if (!startInfo.UseShellExecute){return this.StartWithCreateProcess(startInfo);}return this.StartWithShellExecuteEx(startInfo);
}这和 .NET Framework 中的实现逻辑基本一致public bool Start()
{this.Close();ProcessStartInfo processStartInfo this.StartInfo;if (processStartInfo.FileName.Length 0){throw new InvalidOperationException(SR.GetString(FileNameMissing));}if (processStartInfo.UseShellExecute){return this.StartWithShellExecuteEx(processStartInfo);}return this.StartWithCreateProcess(processStartInfo);
}那么问题出在哪呢通过 dnspy 调试 .NET Framework 版本的测试程序我们发现最后执行的是StartWithShellExecuteEx而不是StartWithCreateProcess方法而之所以走不同的逻辑分支是由processStartInfo.UseShellExecute控制的。所以解决方案也很简单设置UseShellExecute true:Process.Start(new ProcessStartInfo(https://baidu.com) { UseShellExecute true });结论造成这样的原因是因为UseShellExecute在 .NET 6 上默认为 falsepublic bool UseShellExecute { get; set; }而在 .NET Framework 上默认为 true:[DefaultValue(true)]
[MonitoringDescription(ProcessUseShellExecute)]
[NotifyParentProperty(true)]
public bool UseShellExecute
{get{return this.useShellExecute;}set{this.useShellExecute value;}
}private bool useShellExecute true;当UseShellExecute false时代码会将传入参数作为文件名使用从而引发“系统找不到指定的文件”异常。添加微信号【MyIO666】邀你加入技术交流群