|
-
Apr 20th, 2011, 07:13 AM
#1
Thread Starter
Fanatic Member
Obtain user's default browser.
Been a while since I posted anything here, thought I'd share a little snippet I wrote up the other day for someone. Just a really simple way to determine the user's default web browser (Chrome, Firefox...etc)
vb.net Code:
Private Function getDefaultBrowser() As String Dim retVal As String = String.Empty Using baseKey As Microsoft.Win32.RegistryKey = My.Computer.Registry.CurrentUser.OpenSubKey("Software\Clients\StartmenuInternet") Dim baseName As String = baseKey.GetValue("").ToString Dim subKey As String = "SOFTWARE\" & IIf(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE") = "AMD64", "Wow6432Node\", "") & "Clients\StartMenuInternet\" & baseName Using browserKey As Microsoft.Win32.RegistryKey = My.Computer.Registry.LocalMachine.OpenSubKey(subKey) retVal = browserKey.GetValue("").ToString End Using End Using Return retVal End Function
A basic walkthrough of the function for those interested:
Firstly, the function opens the registry key @ HKEY_CURRENT_USER\Sofware\Clients\StartmenuInternet
The default value at this registry key is the executable name of your default browser (IEXPLORE9.EXE, FIREFOX.EXE, Chrome.exe...etc), these names aren't overly helpful as it's not the actual browser's "name". However, they do help out, so we will store this in the "baseName" variable for later use.
The next step is to build our next registry location. Due to the difference in registry keys on a 32 bit vs 36 bit system the ternary "IIf" operator was used here to apply the "Wow6432Node" if necessary (64 bit systems). The StartMenuInternet key contains multiple sub keys, one for each browser you have installed, coincidentally the subkey names correspond to the value stored in baseName, so we use that variable to open the "default browser's" folder.
This is the moneymaker, the default value at this key is the actual name of the browser, so we just assign retVal the default key value and call it quits.
Return retVal <- self explanatory, return the value we found.
(Not yet tested on a 32 bit system so if someone finds an error please point it out)
Example of basic use:
vb.net Code:
MessageBox.Show(String.Concat("Your default browser is: ", getDefaultBrowser(), "."), "Your default browser")
Cheers,
Jason.
If I helped you out, please take the time to rate me 
-
Apr 24th, 2011, 11:55 PM
#2
Thread Starter
Fanatic Member
Re: Obtain user's default browser.
No-one going to critique any of the code? Damn.
If I helped you out, please take the time to rate me 
-
May 6th, 2011, 03:06 PM
#3
New Member
Re: Obtain user's default browser.
I just stumbled across your post, figured I'd reply.
From
http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
-------------------------------------------------
Registering for the Start Menu Internet Link
Being registered as the default Start menu Internet application is not the same as being registered as the default web browser. The default web browser is used for launching arbitrary URLs from anywhere in the system. The Start menu Internet application merely controls the program that is started when the user clicks the Internet icon on the Start menu.
Any web browser application can register to appear as an Internet client on the Start menu. This visibility, coupled with proper registration for an application's file and protocol types, gives an application default browser status.
------------------------------------------------
HKCU\Software\Clients\ doesn't exist in Win7 by default, because while IE is the default browser, there is no longer the "Start | Internet" option.
If you set Firefox as the default, FF creates the key (assuming it's a legacy function). If you change the default back to IE, then IE does change the StartMenuInternet\Default value (probably just to undo changes made by FF).
In either case, there's no HKCU\Software\Wow6432Node\Clients\ key on a Win7 x64 system. There is HKLM\Software\Wow6432Node\Clients by default, but that doesn't get changed when a user changes their default browser.
So the code won't work on Win7 x64. It will only detect FF as default on Win7 x32, and IE only if they've switched back from IE. And only if they set the default browser through the browser interface; if they set the default browser by changing the file association, then the MenuStartInternet is not going to change.
Cheers!
-
May 7th, 2011, 12:28 AM
#4
Thread Starter
Fanatic Member
Re: Obtain user's default browser.
 Originally Posted by q_t_m
I just stumbled across your post, figured I'd reply.
From
http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
-------------------------------------------------
Registering for the Start Menu Internet Link
Being registered as the default Start menu Internet application is not the same as being registered as the default web browser. The default web browser is used for launching arbitrary URLs from anywhere in the system. The Start menu Internet application merely controls the program that is started when the user clicks the Internet icon on the Start menu.
Any web browser application can register to appear as an Internet client on the Start menu. This visibility, coupled with proper registration for an application's file and protocol types, gives an application default browser status.
------------------------------------------------
HKCU\Software\Clients\ doesn't exist in Win7 by default, because while IE is the default browser, there is no longer the "Start | Internet" option.
If you set Firefox as the default, FF creates the key (assuming it's a legacy function). If you change the default back to IE, then IE does change the StartMenuInternet\Default value (probably just to undo changes made by FF).
In either case, there's no HKCU\Software\Wow6432Node\Clients\ key on a Win7 x64 system. There is HKLM\Software\Wow6432Node\Clients by default, but that doesn't get changed when a user changes their default browser.
So the code won't work on Win7 x64. It will only detect FF as default on Win7 x32, and IE only if they've switched back from IE. And only if they set the default browser through the browser interface; if they set the default browser by changing the file association, then the MenuStartInternet is not going to change.
Cheers!
Alright, I use windows Vista and did some checking by changing my default browser from IE to FF to Chrome to Safari and back to IE and this snippet worked fine in most cases, guess it doesn't work on Win7, shame.
If I helped you out, please take the time to rate me 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|