Opening default web browser

后端 未结 5 1463
臣服心动
臣服心动 2020-12-21 08:05

I am using the function below to open the user\'s default web browser.

 Public Function ShowHelp(ByVal url As String) As System.Diagnostics.Process
    Dim s         


        
相关标签:
5条回答
  • 2020-12-21 08:29

    This is in C#, but this is a good article:

    http://ryanfarley.com/blog/archive/2004/05/16/649.aspx

    Here's the C# as VB.NET:

    Private Function getDefaultBrowser() As String
        Dim browser As String = String.Empty
        Dim key As RegistryKey = Nothing
        Try
            key = Registry.ClassesRoot.OpenSubKey("HTTP\shell\open\command", False)
    
            'trim off quotes
            browser = key.GetValue(Nothing).ToString().ToLower().Replace("""", "")
            If Not browser.EndsWith("exe") Then
                'get rid of everything after the ".exe"
                browser = browser.Substring(0, browser.LastIndexOf(".exe") + 4)
            End If
        Finally
            If key IsNot Nothing Then
                key.Close()
            End If
        End Try
        Return browser
    End Function
    
    0 讨论(0)
  • 2020-12-21 08:31

    If you're running on Windows, the following command-line should work from anywhere:

    rundll32 url.dll,FileProtocolHandler <your_url>
    

    where <your_url> is the webpage URL to navigate to.

    Public Function ShowHelp(ByVal url As String) As System.Diagnostics.Process
            Dim startInfo As New Diagnostics.ProcessStartInfo()
            startInfo.FileName = "rundll32 url.dll,FileProtocolHandler"
            startInfo.Arguments = url
            startInfo.WindowStyle = ProcessWindowStyle.Maximized
            Return System.Diagnostics.Process.Start(startInfo)
    End Function
    
    0 讨论(0)
  • 2020-12-21 08:33

    If you wish to dislay a file the ends in ".html" or "htm" then you can just pass it to the Process.Start() method. The same may work with a URL.

    (You must have the flag set that gets Process.Start() to use the shell methods.)

    0 讨论(0)
  • 2020-12-21 08:38
     Private Sub helpRichTextBox_LinkClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.LinkClickedEventArgs) Handles helpRichTextBox.LinkClicked
            System.Diagnostics.Process.Start(e.LinkText)
        End Sub
    
    0 讨论(0)
  • 2020-12-21 08:41

    That's the right way to launch the browser with a URL in general, but if it fails, I would just catch that specific exception, and then attempt to call iexplore <url> to open the URL in IE, given that it is bound to be installed on any Windows system. (I assume you're not targeting Mono/Linux here.)

    0 讨论(0)
提交回复
热议问题