How to retrieve default browsers path in C#

csharp_standard_browserBelow it will be about how to find the path to the default browser on Windows systems with help of C#. This makes sense, if you want to open a file using Process.Start () in the default browser.

Although one might assume that every Windows user has installed Internet Explorer and you should therefore simply use this to view files, but it does not necessarily increase the confidence of the user into the own application. So at least I, as a user, would be annoyed of my program, if this would always launch Internet Explorer instead of Firefox, which I have set as the default browser.

Now there are two ways to open the default browser in C #. Both have their advantages and disadvantages.

Option 1 – Process.Start() with an URL

The easiest way to launch the default browser is to start a new process using the Process-class. As startpath for the process the url is passed.

string url = "http://google.de";
Process.Start(url);

These two lines are enough to open the specified url in the default browser. However, the problem is if the variable “url” is not a valid url. In this case there may be an exception thrown, because the. NET Framework does not know in every case which application has to be started by the passed parameter.

This can be, for example, if you have no influence on the url parameter and no matter what it is, but want to open it nevertheless in any case with the default browser. For this case option 2 is suitable.

Option 2 – Get the default browsers path in C#

To open the default browser in any case and let the browser decide whether it can open or view a file/url, it is necessary to know the path of the default browser. For this purpose, you can use the following small function that reads the path of the default browser from the registry.

private static string GetStandardBrowserPath()
{
    string browserPath = string.Empty;
    RegistryKey browserKey = null;

    try
    {
        //Read default browser path from Win XP registry key
        browserKey = Registry.ClassesRoot.OpenSubKey(@"HTTP\shell\open\command", false);

        //If browser path wasn't found, try Win Vista (and newer) registry key
        if (browserKey == null)
        {
            browserKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http", false); ;
        }

        //If browser path was found, clean it
        if (browserKey != null)
        {
            //Remove quotation marks
            browserPath = (browserKey.GetValue(null) as string).ToLower().Replace("\"", "");

            //Cut off optional parameters
            if (!browserPath.EndsWith("exe"))
            {
                browserPath = browserPath.Substring(0, browserPath.LastIndexOf(".exe") + 4);
            }

            //Close registry key
            browserKey.Close();
        }
    }
    catch
    {
        //Return empty string, if no path was found
        return string.Empty;
    }
    //Return default browsers path
    return browserPath;
}

This function is now used once again by the Process.Start()-function. The url this time is passed as parameter and not as the path of the executable which should be started.

string url = "http://google.de";
string browserPath = GetStandardBrowserPath();
if (string.IsNullOrEmpty(browserPath))
{
    MessageBox.Show("No default browser found!");
}
else
{
    Process.Start(browserPath, url);
}

Conclusion

 

That’s it. So if you’re sure that the urls are in a valid format, you should use option 1. If you can not ensure this, or maybe want to open files in your own format or with your own extension, so option 2 is suitable.

In Variant 2 you should keep in mind that your application, depending on the user context in which it is executed, explicitly requires administrator rights!

How do you deal with the “problem”? Do you also have a solution/function that is based on the registry or are you going a completely different way?

 

2 Comments

  1. levansays:

    many thanks you are great man

  2. As you stated you can just open a web page in the default browser by calling `Process.Start( url )`. Wouldn’t it be easier to verify whether it is a valid url (perhaps using the Uri class?) prior to this call?

    However, I also needed the path to the default browser since I needed to pass custom command line arguments. You helped me greatly by mentioning the key “Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http”. The full key is “Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice” and the default browser is stored in the “Progid” value. This seems to be the preferred way to set default programs starting from Windows Vista. The “Progid” value doensn’t store a path though, so I’m quite certain your posted code won’t work when “HTTP\shell\open\command” isn’t present. Additionally, this key isn’t updated for me on Windows 7 when IE is set as the default browser.

    I posted a code sample to get the default browser and path on Stack Overflow using the newer key:
    http://stackoverflow.com/a/17599201/590790

Leave a Reply to Steven Jeuris Cancel reply

Please be polite. We appreciate that. Your email address will not be published and required fields are marked