Code: Open URL by a New Browser

简介:

C#

private  void OpenUrl( string url)
{
     string browser = GetDefaultBrowser();
     if ( browser.Length > 0 )
     {
        ProcessStartInfo psi = new ProcessStartInfo();
        psi.UseShellExecute =  false;
        psi.Arguments = url;
        psi.FileName = browser;
        System.Diagnostics.Process.Start(psi);
    }
     else
     {
        System.Diagnostics.Process.Start(url);
    }
}

private string GetDefaultBrowser()
{
     string browser = String.Empty;
    RegistryKey key =  null;
     try
     {
        key = Registry.ClassesRoot.OpenSubKey(@"HTTP\shell\open\command",  false);
        browser = key.GetValue( null).ToString().ToLower().Replace("\"", "");
         if ( !browser.EndsWith(".exe") )
         {
            browser = browser.Substring(0, browser.LastIndexOf(".exe") + 4);
        }
    }
     catch
     {
         if ( key !=  null )
         {
            key.Close();
        }
    }
    return browser;
}


VB.NET

Private Sub OpenUrl( ByRef url As String )
    Dim browser As  String = GetDefaultBrowser()
     If browser.Length > 0  Then
        Dim psi  As ProcessStartInfo =  New ProcessStartInfo
        psi.UseShellExecute =  False
        psi.Arguments = url
        psi.FileName = browser
        System.Diagnostics.Process.Start(psi)
     Else
        System.Diagnostics.Process.Start(url)
     End  If
End Sub

Private  Function GetDefaultBrowser()
     Dim browser  As String =  String.Empty
     Dim key  As RegistryKey
     Try
        key = Registry.ClassesRoot.OpenSubKey("HTTP\shell\open\command",  False)
        browser = key.GetValue( Nothing).ToString().ToLower().Replace("""", "")
         If  Not browser.EndsWith(".exe")  Then
            browser = browser.Substring(0, browser.LastIndexOf(".exe") + 4)
         End  If
     Catch ex  As Exception
         If  Not key Is  Nothing Then
            key.Close()
         End  If
     End  Try
    GetDefaultBrowser = browser
End Function

本文转自博客园鸟食轩的博客,原文链接:http://www.cnblogs.com/birdshome/,如需转载请自行联系原博主。

目录
相关文章
POST 请求出现异常!java.io.IOException: Server returned HTTP response code: 400 for URL
POST 请求出现异常!java.io.IOException: Server returned HTTP response code: 400 for URL
1144 0
|
5月前
|
前端开发 Linux Docker
【源码共读】跨平台打开 URL、文件、可执行文件 open
【源码共读】跨平台打开 URL、文件、可执行文件 open
74 1
|
开发工具 git
Gitlab提交代码:You are not allowed to push code to this project.fatal: unable to access requested URL
Gitlab提交代码:You are not allowed to push code to this project.fatal: unable to access requested URL
839 0
Useful code snippet to parse the key value pairs in URL
Useful code snippet to parse the key value pairs in URL
116 0
Useful code snippet to parse the key value pairs in URL
SAP Spartacus里product code出现在url的几种场景
SAP Spartacus里product code出现在url的几种场景
SAP Spartacus里product code出现在url的几种场景
|
Web App开发 Java 网络安全
java.io.IOException: Server returned HTTP response code: 403 for URL: http://start.spring.io
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; color: #454545} span.s1 {font: 12.0px '.PingFang SC'} 403 Forbidden 是HTTP协议中的一个状态码(Status Code)。
4600 0
|
Java Spring
java.io.IOException: Server returned HTTP response code: 415 for URL:xxxxxx
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; color: #454545} span.s1 {text-decoration: underline} span.
2386 0
|
数据格式 XML
How to let FIR open a URL when you click an image
When we use FIR as a ad bar, we want FIR can take the visitor to specific web page when visitor click the image.
586 0