Link to home
Create AccountLog in
Visual Basic Classic

Visual Basic Classic

--

Questions

--

Followers

Top Experts

Avatar of Ryan_R
Ryan_R🇦🇺

VB Function to find default Internet Browser
I'm looking for an quick function that will return a path and app name for the Default Internet Browser - so that I don't upset Firefox users  :)

ie - sRtn = "C:\Program Files\Internet Explorer\iexplore.exe"

VB.NET 2005 - This needs to support Win Vista 32bit


Thanks

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


ASKER CERTIFIED SOLUTION
Avatar of Jason EvansJason Evans🇬🇧

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of Ryan_RRyan_R🇦🇺

ASKER

Thought it might be something like that - thanks.

As soon as I test it I'll give you the points

Avatar of Ryan_RRyan_R🇦🇺

ASKER

<<Dim registryKey As RegistryKey>>  - Type 'RegistryKey' is unknown to vb.net 05 express
<<registryKey = Registry.ClassesRoot.OpenSubKey(key, False)>>  - Name 'registry' is not declared

SOLUTION
Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.

Avatar of Ryan_RRyan_R🇦🇺

ASKER

Thanks

I did a google on it and found that out too just earlier

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of icricr🇬🇧

The suggested method doesn't really work a lot of the time. My default browser is Firefox, but html files are for some reason associated with IE.

If you just need to find *a* browser, it will work fine. If you want to find the default browser it will give erroneous results for a lot of people.

Perhaps a better way to do it is as follows:

Private Function GetDefaultBrowserPath() As String
    Dim StartMenuInternetKey As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Clients\StartMenuInternet", False)
    Dim InternetProgram As String = StartMenuInternetKey.GetValue(Nothing)
    Return StartMenuInternetKey.OpenSubKey(InternetProgram + "\shell\open\command", False).GetValue(Nothing, Nothing)
End Function

This will get the Browser that is pinned to the top of most peoples Start Menu. I think it's probably Windows XP only though.

Avatar of icricr🇬🇧

I extended my code a bit to handle both when a user specifies their own default browser (instead of using the system default) and to fall back to getting the browser from the HTML extension if there is a problem (usually because they are using Windows 98/2000).

    ''' <summary>
    ''' Gets the path to the default browser from the registry.
    ''' </summary>
    ''' <returns>The path to the default browser. null (Nothing in VB) if not set.</returns>
    Function GetDefaultBrowserPath() As String
        Dim startMenuInternetSubKey As String = "Software\Clients\StartMenuInternet"

        ' Find the key in the registry that contains the relevent default browser information.
        ' This is either in the current user if they have a different one to the local machine, otherwise
        ' in local machine.
        Dim startMenuInternetKey As RegistryKey = Registry.CurrentUser.OpenSubKey(startMenuInternetSubKey, False)
        ' If the current user does not have a default browser set, use the default browser for the local machine.
        If startMenuInternetKey Is Nothing Then
            startMenuInternetKey = Registry.LocalMachine.OpenSubKey(startMenuInternetSubKey, False)
        End If

        ' Get the name of the default clients subkey and get the shell open command. This may be null if, for some
        ' strange reason, it does not exist.
        Dim defaultBrowser As Object = startMenuInternetKey.GetValue(Nothing, Nothing)

        ' Handle null cases (this is odd and shouldn't really happen. But theoretically it might.
        If defaultBrowser Is Nothing Then
            Return GetDefaultBrowserPathFromHtmlExtension()
        End If

        Dim defaultBrowserPath As Object = startMenuInternetKey.OpenSubKey(String.Format("{0}\shell\open\command", defaultBrowser), False).GetValue(Nothing, Nothing)

        ' Handle null cases when casting and returning.
        If defaultBrowserPath Is Nothing Then
            Return GetDefaultBrowserPathFromHtmlExtension()
        End If

        Return defaultBrowserPath
    End Function

    ''' <summary>
    ''' Gets the path to the browser associated with html files.
    ''' </summary>
    ''' <returns>The path to the browser associated with html files. null (Nothing in VB) if not set.</returns>
    ''' <remarks>Often used as a fallback for GetDefaultBrowserPath.</remarks>
    Function GetDefaultBrowserPathFromHtmlExtension()
        ' Used as a fall back method if the normal GetDefaultBrowserPath fails.
        ' Often this is because they are using Windows 98/2000.

        ' Open the registry key that has the command to open the program associated with html files.
        Dim htmlShellOpenKey As RegistryKey = Registry.ClassesRoot.OpenSubKey("htmlfile\shell\opennew\command")

        ' If for some reason this key doesn't exist, return null.
        If htmlShellOpenKey == Nothing Then
            Return Nothing
        End If

        Return htmlShellOpenKey.GetValue(Nothing, Nothing)
    End Function

Avatar of Ryan_RRyan_R🇦🇺

ASKER

Thanks for that effort (expecially after the Q was PAQed) - much appreciated.

One small note regarding  >>I think it's probably Windows XP only though.<<
This program will be run on Vista only - there will be no need to run it on XP or any other OS.

I'll wait for your reply before trying out your second idea.  :)

I'm happy to reopen this Q to include you in a split as well (after increasing points) so please treat this as if it's still open.

Ryan

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of Ryan_RRyan_R🇦🇺

ASKER

Hi - I have found a much better solution for tis question (for future readers). (The points are still yours for helping out).

The following opens the website in the default browser:

Process.Start("http://www.website.com")


Ryan R

with 19298082

 If htmlShellOpenKey == Nothing Then
            Return Nothing
        End If

gives me an error in vb 2012, doesn't like the ==

papaaT555
Visual Basic Classic

Visual Basic Classic

--

Questions

--

Followers

Top Experts

Visual Basic is Microsoft’s event-driven programming language and integrated development environment (IDE) for its Component Object Model (COM) programming model. It is relatively easy to learn and use because of its graphical development features and BASIC heritage. It has been replaced with VB.NET, and is very similar to VBA (Visual Basic for Applications), the programming language for the Microsoft Office product line.