Click here to Skip to main content
15,120,477 members
Articles / General Programming / Internet
Alternative
Tip/Trick
Posted 17 Aug 2011

Stats

10.3K views
1 bookmarked

Opening the Internet Browser Programmatically

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
17 Aug 2011CPOL
If I remember correctly, then I had some issues with the above method some years ago. It would not always work on some systems.I personally use the following method to get the EXE path to the system's default browser:public static string GetDefaultBrowser(){ string browser =...
If I remember correctly, then I had some issues with the above method some years ago. It would not always work on some systems.

I personally use the following method to get the EXE path to the system's default browser:

C#
public static string GetDefaultBrowser()
{
    string browser = string.Empty;
    Microsoft.Win32.RegistryKey key = null;
    try
    {
	object obj = null;
	key = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(@"HTTP\shell\open\command", false);
	if (key != null)
	{
	    obj = key.GetValue(null);
	    if (obj != null)
	    {
		browser = Convert.ToString(obj);
	    }
	}

	if(string.IsNullOrEmpty(browser))
	{
	    key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Classes\Applications\iexplore.exe\shell\open\command", false);

	    obj = key.GetValue(null);
	    if (obj != null)
	    {
		browser = Convert.ToString(obj);
	    }
	}

	if (!string.IsNullOrEmpty(browser))
	{
	    //trim off quotes
	    browser = key.GetValue(null).ToString().ToLower().Replace("\"", "");
	    if (!browser.EndsWith("exe"))
	    {
		//get rid of everything after the ".exe"
		browser = browser.Substring(0, browser.LastIndexOf(".exe") + 4);
	    }

	    //Enable quotes again
	    browser = '"' + browser + '"';

	    return browser;
	}
    }
    catch
    {
	//noop
    }
    finally
    {
	if (key != null) key.Close();
    }

    return string.Empty;
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Michael B. Hansen
Founder CWAIN
Denmark Denmark
I have a MCPD .NET 3.5 EAD and a Bsc in computer science, organizational theory and economics.
After more than 15 years working as professional system engineer, I founded the independent consultant agency CWAIN.

Comments and Discussions

 
GeneralIt is a good way for c#. My way is for beginners :) Pin
Caner Korkmaz18-Aug-11 22:17
MemberCaner Korkmaz18-Aug-11 22:17 
GeneralGood point. I don't normally bother with this that much, as ... Pin
Michael B. Hansen18-Aug-11 0:08
MemberMichael B. Hansen18-Aug-11 0:08 
GeneralThis approach would require you to have permissions to read ... Pin
Reiss17-Aug-11 23:44
professionalReiss17-Aug-11 23:44 
This approach would require you to have permissions to read the registry - not wrong, but required - http://msdn.microsoft.com/en-us/library/system.security.permissions.registrypermission.aspx

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.