How to get the path to the users default browser?

  • Thread starter Thread starter Darious Snell
  • Start date Start date
D

Darious Snell

I need to be able to get the path to the users default browser from within my application.

I am in a situation where I can't easily use System.Diagnostics.Process.Start to start
their browser with the URL I want and I must resort to using "Shell". Of course, shell
wants to know where the browser is and now you can see my dilemma!

Thanks in advance...
 
You will need to query this registry path:

HKEY_CLASSES_ROOT\http\shell\open\command

The default key contains the path
 
Sorry, but forgot to add this one too:

HKEY_LOCAL_MACHINE\SOFTWARE\Classes\http\shell\open\command
 
¤ I need to be able to get the path to the users default browser from within my application.
¤
¤ I am in a situation where I can't easily use System.Diagnostics.Process.Start to start
¤ their browser with the URL I want and I must resort to using "Shell". Of course, shell
¤ wants to know where the browser is and now you can see my dilemma!

Give the following a try:

Declare Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" (ByVal lpFile As
String, _
ByVal lpDirectory As
String, _
ByVal lpResult As
System.Text.StringBuilder) As Int32

Function DefaultBrowserPath() As String

Dim DummyFile As String
Dim FileDir As String

Dim FilePath As New System.Text.StringBuilder(255)
DummyFile = "e:\My Documents\dummy.htm"

If FindExecutable(DummyFile, FileDir, FilePath) > 32 Then
DefaultBrowserPath = FilePath.ToString
End If

End Function


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
Just a quick note...

An example of what can be stored in that key...

C:\PROGRA~1\MOZILL~1\FIREFOX.EXE -url "%1"

There needs to be a little string manipulation before using
this in a "shell" command. I came up with
Dim MyString As String
Dim MySplitStrings() As String
Dim ExeString As String
Dim BrowserRegKey As String = "SOFTWARE\Classes\http\shell\open\command"
Dim rk As RegistryKey = Registry.LocalMachine.OpenSubKey(BrowserRegKey, False)
MyString += CType(rk.GetValue(""), String)
MySplitStrings = MyString.Split(" ")
ExeString = MyStrings(0).ToString

This works with IE and Firefox. Probably most anything else as well.
I am sure there are more elegant ways to do this. But, this works for
the moment.

Thanks
 
I need to be able to get the path to the users default browser from within my application.

I am in a situation where I can't easily use System.Diagnostics.Process.Start to start
their browser with the URL I want and I must resort to using "Shell". Of course, shell
wants to know where the browser is and now you can see my dilemma!

Thanks in advance...

I'm a little confused... If you have a URL, you don't even need to know
their default browser using System.Diagnostics.Process. You just set
the filename property of the startinfo structure to the URL and make
sure useshellexectue is true. The system will start the default browser
automatically.

Dim browser As New Process ()
browser.StartInfo.FileName = "http://www.microsoft.com"
browser.StartInfo.UseShellExecute = True
browser.Start ()
 
The reason I am having difficulty using .start ----

"The requested section is not present in the activation context"

This shows up due to threading model issues with the architecture.
I couldn't resolve the threading issues so I ended up using shell.
I tried using STAThread/MTAThread etc but I couldn't clear the issue.
So, "shell" produces the desired outcome. But, you are right, my first
choice was to go with the method you describe below.
 
Back
Top