views:

347

answers:

5

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 startInfo As New Diagnostics.ProcessStartInfo()
            startInfo.FileName = url
            startInfo.WindowStyle = ProcessWindowStyle.Maximized
            Return System.Diagnostics.Process.Start(startInfo)
 End Function

A couple of times the function returned the error (on users machine) "the system cannot find the file specified"

I guess the user has not set a default web browswer. Why i get this error? How could i add a default web browswer check before calling this function?

+1  A: 

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
Nissan Fan
A: 

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.)

Noldorin
Various legal cases have forced Microsoft to create a number of versions of Windows without IE - you can't assume it's present.
MarkJ
@MarkJ: Ah yes, I do remember that now. Still, more often than not, it exists. A fair last resort, in my view.
Noldorin
+1  A: 

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
Ian Kemp
A: 

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.)

Ian Ringrose
A: 
 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
Geoffrey Van Wyk