我正在尝试从Windows窗体提供指向我公司网站的链接.我希望表现良好,并使用用户首选的浏览器启动.
从Windows窗体应用程序在用户的默认浏览器中打开URL的最佳方法是什么?
Blo*_*ard 129
该文章将引导您完成它.
简短回答:
ProcessStartInfo sInfo = new ProcessStartInfo("http://mysite.com/");
Process.Start(sInfo);
Run Code Online (Sandbox Code Playgroud)
Aar*_*ner 33
using System.Diagnostics;
Process.Start("http://www.google.com/");
Run Code Online (Sandbox Code Playgroud)
这种方法对我有用,但我可能会遗漏一些重要的东西.
这是两个世界中最好的:
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
Run Code Online (Sandbox Code Playgroud)
我发现当在Windows 8设备上运行桌面应用程序时,Blorgbeard提供的答案将失败.对于Camillo而言,您应该尝试使用用户的默认浏览器应用程序打开它,但如果未分配browswer应用程序,则会抛出未处理的异常.
我将此作为答案发布,因为它在尝试打开默认浏览器中的链接时处理异常.
对于那些收到“Win32Exception:系统找不到指定的文件”的人
这应该做的工作:
ProcessStartInfo psInfo = new ProcessStartInfo
{
FileName = "https://www.google.com",
UseShellExecute = true
};
Process.Start(psInfo);
Run Code Online (Sandbox Code Playgroud)
UseShellExecute在这里进一步描述
对我来说,问题是由于这里描述的 .NET 运行时