【发布时间】:2019-10-03 06:32:15
【问题描述】:
我有以下代码在默认浏览器中打开 URL:
string url;
//...
Process.Start(url);
但它会失败并抛出带有一些 URL 的 Win32Exception,例如:
https://tw.news.yahoo.com/%E6%95%B8%E4%BD%8D%E8%BA%AB%E5%88%86%E8%AD%89%E6%93%AC9%E6%9C%88%E6%8F%90%E6%A8%A3%E5%BC%B5-%E5%BE%90%
堆栈跟踪是这样的:
System.ComponentModel.Win32Exception (0x80004005): The system cannot find the file specified.
at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start()
at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start(String fileName)
at MyApp.GoURL(String url)
我一直在将默认浏览器从 firefox 切换到 chrome、edge、brave 等。
我在this dotnet issue 中尝试了一些解决方法,
Process.Start(
new ProcessStartInfo("cmd", $"/c start {url}")
{ CreateNoWindow = true });
或
ProcessStartInfo psi = new ProcessStartInfo
{
FileName = url,
UseShellExecute = true
};
Process.Start(psi);
但仍然没有运气,无法打开我的默认浏览器。报错信息还是The system cannot find the file specified.
有一些 solutions 正在打开 Internet Explorer,但它们不符合我的规范。
如何在任何默认浏览器中打开这样的网址?
【问题讨论】:
-
错误消息说它找不到文件。这很可能是由于给定的路径不正确。
-
@Daredevil 我认为它应该打开我的默认浏览器,如果 URL 不正确则显示 404 页面?
-
可能是,但你能发布整个堆栈跟踪错误吗?
-
@Daredevil 我将堆栈跟踪添加到我的帖子中。
标签: c# windows browser process system.diagnostics