.net 从 Windows 窗体打开 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/58024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Open a URL from Windows Forms
提问by Adrian Clark
I'm trying to provide a link to my company's website from a Windows Form. I want to be well behaved and launch using the user's preferred browser.
我正在尝试从 Windows 窗体提供到我公司网站的链接。我想表现得很好,并使用用户首选的浏览器启动。
What is the best way to open a URL in the user's default browser from a Windows Forms application?
从 Windows 窗体应用程序在用户的默认浏览器中打开 URL 的最佳方法是什么?
回答by Blorgbeard is out
回答by Aaron Wagner
using System.Diagnostics;
Process.Start("http://www.google.com/");
This approach has worked for me, but I could be missing something important.
这种方法对我有用,但我可能会遗漏一些重要的东西。
回答by Rogala
Here is the best of both worlds:
这是两全其美的:
Dim sInfo As New ProcessStartInfo("http://www.mysite.com")
Try
Process.Start(sInfo)
Catch ex As Exception
Process.Start("iexplore.exe", sInfo.FileName)
End Try
I found that the answer provided by Blorgbeard will fail when a desktop application is run on a Windows 8 device. To Camillo's point, you should attempt to open this with the user's default browser application, but if the browswer application is not assigned, an unhandled exception will be thrown.
我发现在 Windows 8 设备上运行桌面应用程序时,Blorgbeard 提供的答案将失败。根据 Camillo 的观点,您应该尝试使用用户的默认浏览器应用程序打开它,但如果未分配浏览器应用程序,则会引发未处理的异常。
I am posting this as the answer since it handles the exception while still attempting to open the link in the default browser.
我将此作为答案发布,因为它在处理异常的同时仍然尝试在默认浏览器中打开链接。
回答by Sumrak
I like approach described here. It takes into account possible exceptions and delays when launching the browser.
我喜欢这里描述的方法。它考虑了启动浏览器时可能出现的异常和延迟。
For best practice make sure you don't just ignore the exception, but catch it and perform an appropriate action (for example notify user that opening the browser to navigate him to the url failed).
对于最佳实践,请确保您不只是忽略异常,而是捕获它并执行适当的操作(例如,通知用户打开浏览器以将其导航到 url 失败)。
回答by ammrin
The above approach is perfect, I would like to recommend this approach to where you can pass your parameters.
上面的方法是完美的,我想推荐这种方法来传递参数。
Process mypr;
mypr = Process.Start("iexplore.exe", "pass the name of website");

