在 C# 中,如何以编程方式使用特定浏览器打开 link?
In C#, how do you programmatically open a link with a specific browser?
例如用户有多个浏览器:Chrome、Firefox、Safari,用户默认浏览器为Chrome。我怎样才能做到当代码打开用户 a link 时,它会使用 Safari 而不是他的默认浏览器,即 Chrome?这可能吗?谢谢:)
试试这个:
System.Diagnostics.Process.Start("Chrome.exe", "http://www.whosebug.com");//or firefox.exe
如果浏览器配置不正确,您可能会遇到异常。所以最好像这样捕获异常:
try
{
System.Diagnostics.Process.Start("Chrome.exe", "http://www.whosebug.com");//or firefox.exe
}
catch(System.ComponentModel.Win32Exception noBrowser)
{
if (noBrowser.ErrorCode == -2147467259)
MessageBox.Show(noBrowser.Message);
}
catch (System.Exception other)
{
MessageBox.Show(other.Message);
}
当应用程序将要启动或 Global.asax 事件时,我们应该处理这种情况,而不是我们只能将请求重定向到任何客户端浏览器。这个技巧甚至不应该尝试 页面事件。
使用名称空间并在全局页面中添加代码:process.Start()
例如用户有多个浏览器:Chrome、Firefox、Safari,用户默认浏览器为Chrome。我怎样才能做到当代码打开用户 a link 时,它会使用 Safari 而不是他的默认浏览器,即 Chrome?这可能吗?谢谢:)
试试这个:
System.Diagnostics.Process.Start("Chrome.exe", "http://www.whosebug.com");//or firefox.exe
如果浏览器配置不正确,您可能会遇到异常。所以最好像这样捕获异常:
try
{
System.Diagnostics.Process.Start("Chrome.exe", "http://www.whosebug.com");//or firefox.exe
}
catch(System.ComponentModel.Win32Exception noBrowser)
{
if (noBrowser.ErrorCode == -2147467259)
MessageBox.Show(noBrowser.Message);
}
catch (System.Exception other)
{
MessageBox.Show(other.Message);
}
当应用程序将要启动或 Global.asax 事件时,我们应该处理这种情况,而不是我们只能将请求重定向到任何客户端浏览器。这个技巧甚至不应该尝试 页面事件。
使用名称空间并在全局页面中添加代码:process.Start()