Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find default web browser using C#?

Tags:

c#

Is there a way I can find out the name of my default web browser using C#? (Firefox, Google Chrome, etc..)

Can you please show me with an example?

like image 600
Hope S Avatar asked Nov 29 '12 08:11

Hope S


People also ask

How do I identify my default browser?

Open the Start menu and type Default apps. Then, select Default apps. In the Default apps menu, scroll down until you see your current default web browser, and click it. In this example, Microsoft Edge is the current default browser.

What is the default window web browser?

Windows 10 comes with the new Microsoft Edge as its default browser. But, if you don't like using Edge as your default internet browser, you can switch to a different browser such as Internet Explorer 11, which still runs on Windows 10, by following these simple steps.


1 Answers

The other answer does not work for me when internet explorer is set as the default browser. On my Windows 7 PC the HKEY_CLASSES_ROOT\http\shell\open\command is not updated for IE. The reason behind this might be changes introduced starting from Windows Vista in how default programs are handled.

You can find the default chosen browser in the registry key, Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice, with value Progid. (thanks goes to Broken Pixels)

const string userChoice = @"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice"; string progId; BrowserApplication browser; using ( RegistryKey userChoiceKey = Registry.CurrentUser.OpenSubKey( userChoice ) ) {     if ( userChoiceKey == null )     {         browser = BrowserApplication.Unknown;         break;     }     object progIdValue = userChoiceKey.GetValue( "Progid" );     if ( progIdValue == null )     {         browser = BrowserApplication.Unknown;         break;     }     progId = progIdValue.ToString();     switch ( progId )     {         case "IE.HTTP":             browser = BrowserApplication.InternetExplorer;             break;         case "FirefoxURL":             browser = BrowserApplication.Firefox;             break;         case "ChromeHTML":             browser = BrowserApplication.Chrome;             break;         case "OperaStable":             browser = BrowserApplication.Opera;             break;         case "SafariHTML":             browser = BrowserApplication.Safari;             break;         case "AppXq0fevzme2pys62n3e0fbqa7peapykr8v":             browser = BrowserApplication.Edge;             break;         default:             browser = BrowserApplication.Unknown;             break;     } } 

In case you also need the path to the executable of the browser you can access it as follows, using the Progid to retrieve it from ClassesRoot.

const string exeSuffix = ".exe"; string path = progId + @"\shell\open\command"; FileInfo browserPath; using ( RegistryKey pathKey = Registry.ClassesRoot.OpenSubKey( path ) ) {     if ( pathKey == null )     {         return;     }      // Trim parameters.     try     {         path = pathKey.GetValue( null ).ToString().ToLower().Replace( "\"", "" );         if ( !path.EndsWith( exeSuffix ) )         {             path = path.Substring( 0, path.LastIndexOf( exeSuffix, StringComparison.Ordinal ) + exeSuffix.Length );             browserPath = new FileInfo( path );         }     }     catch     {         // Assume the registry value is set incorrectly, or some funky browser is used which currently is unknown.     } } 
like image 92
Steven Jeuris Avatar answered Sep 28 '22 15:09

Steven Jeuris