在Windows 8/Chrome上,Process.Start(url)无法正常工作 - 是否有替代方法?

23

从.NET应用程序中打开URL,许多网站(包括StackOverflow)引用了这个例子:

Process.Start("http://www.google.com/");

在Windows 8上,如果Internet Explorer是默认浏览器,则此方法有效。然而,如果Google Chrome是默认浏览器,则会出现以下错误:

Unhandled Exception: System.ComponentModel.Win32Exception: Class not registered

这是否意味着该方法不再是在Windows上打开URL的正确方式?有哪些替代方案?直接启动Internet Explorer是否更安全?

5个回答

27

5
当URL包含特殊字符,如= 时,需要进行转义:Process.Start("explorer.exe", $"\"{url}\""); - xmedeko

1

使用启动器对象打开URL。

示例:

await Launcher.LaunchUriAsync(new Uri("www.google.com"));

1

我尝试了很多解决方案,但由于我在一个UI项目中(wpf或winform),最终使用了嵌入式浏览器控件。调用navigate函数,设置url然后将target设置为"_blank"可以启动一个外部浏览器窗口。

_webBrowser.Navigate(uri, "_blank");

希望这有所帮助。

DC


0

0

VB.Net的替代方案:

    Public Sub URLOpen(Url As String)

       Dim OpenURL As New ProcessStartInfo With {
        .UseShellExecute = True,
        .FileName = "explorer.exe",
        .Arguments = Url
       }

       Process.Start(OpenURL)

    End Sub

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接