Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
90 views
in Technique[技术] by (71.8m points)

How to find default web browser using 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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_ROOThttpshellopencommand 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, SoftwareMicrosoftWindowsShellAssociationsUrlAssociationshttpUserChoice, with value Progid. (thanks goes to Broken Pixels)

const string userChoice = @"SoftwareMicrosoftWindowsShellAssociationsUrlAssociationshttpUserChoice";
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 + @"shellopencommand";
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.
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...