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/,如需转载请自行联系原博主。

目录
打赏
0
0
0
0
20
分享
相关文章
Open NotebookLM,一键PDF/URL转播客!
本文带你来了解,结合不同的开源模型,例如Qwen2.5-72B-Instruct, CosyVoice-300M)等,将PDF文件(比如论文paper),或者网页URL内容,转换成为有趣的播客😊。
【源码共读】跨平台打开 URL、文件、可执行文件 open
【源码共读】跨平台打开 URL、文件、可执行文件 open
99 1
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
981 0
POST 请求出现异常!java.io.IOException: Server returned HTTP response code: 400 for URL
POST 请求出现异常!java.io.IOException: Server returned HTTP response code: 400 for URL
4314 0
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.
603 0
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.
2548 0
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)。
4668 0
前端JS截取url上的参数
文章介绍了两种前端JS获取URL参数的方法:手动截取封装和使用URLSearchParams。
79 0
Asp.net Webapi 的 Post 方法不能把参数加到 URL 中?试试这样写
Asp.net Webapi 的 Post 方法不能把参数加到 URL 中?试试这样写

热门文章

最新文章