65.9K
CodeProject is changing. Read more.
Home

Opening the Internet Browser Programmatically

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Aug 18, 2011

CPOL
viewsIcon

13380

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:
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;
}