How to Find Default Web Browser Using C#

How to find default web browser using C#?

You can look here for an example, but mainly it can be done like this:

internal string GetSystemDefaultBrowser()
{
string name = string.Empty;
RegistryKey regKey = null;

try
{
//set the registry key we want to open
regKey = Registry.ClassesRoot.OpenSubKey("HTTP\\shell\\open\\command", false);

//get rid of the enclosing quotes
name = regKey.GetValue(null).ToString().ToLower().Replace("" + (char)34, "");

//check to see if the value ends with .exe (this way we can remove any command line arguments)
if (!name.EndsWith("exe"))
//get rid of all command line arguments (anything after the .exe must go)
name = name.Substring(0, name.LastIndexOf(".exe") + 4);

}
catch (Exception ex)
{
name = string.Format("ERROR: An exception of type: {0} occurred in method: {1} in the following module: {2}", ex.GetType(), ex.TargetSite, this.GetType());
}
finally
{
//check and see if the key is still open, if so
//then close it
if (regKey != null)
regKey.Close();
}
//return the value
return name;

}

Get the default browser, check if open and send URL

System.Diagnostics.Process.Start(startUrl); 

thats all. It checks if default browser is open, run it if not and open the url in new tab and activates it.

How to open in default browser in C#

You can just write

System.Diagnostics.Process.Start("http://google.com");

EDIT: The WebBrowser control is an embedded copy of IE.

Therefore, any links inside of it will open in IE.

To change this behavior, you can handle the Navigating event.

C# Launch default browser with a default search query

If you already know how to find the default browser, I would try using Process.Start("browser\path.exe", "\"? searchterm\"");

This seems to work for both IE and Chrome.

How to get icon of default browser in C#?

I have changed the code a bit and here is my version.

using System;
using Microsoft.Win32;
using System.Drawing;

namespace Namespace
{
public static class DefaultSystemBrowser
{
private static bool initialized = false;
private static string path = null;

public static string Path
{
get
{
CheckForErrors();

return path;
}
}

public static Icon Icon
{
get {
CheckForErrors();

return Icon.ExtractAssociatedIcon( path );
}
}

public static Bitmap Bitmap
{
get
{
CheckForErrors();

return Icon.ExtractAssociatedIcon( path ).ToBitmap();
}
}

private static void CheckForErrors()
{
if ( !initialized )
throw new InvalidOperationException( "You can't use DefaultSystemBrowser class before you call Determine()." );
if ( ErrorMessage != null )
throw new InvalidOperationException( "You can't use DefaultSystemBrowser class if call to Determine() resulted in error." );
}

/// <summary>
/// Null if no error occured, error description otherwise.
/// </summary>
public static string ErrorMessage
{
get;
private set;
}

/// <summary>
/// Finds out all information about current default browser. You can call this method every time you want to find out default browser.
/// </summary>
public static void Determine()
{
path = String.Empty;
initialized = true;

RegistryKey regKey = null;
ErrorMessage = null;

try
{
//set the registry key we want to open
regKey = Registry.ClassesRoot.OpenSubKey( "HTTP\\shell\\open\\command", false );

//get rid of the enclosing quotes
path = regKey.GetValue( null ).ToString().ToLower().Replace( "" + (char) 34, "" );

//check to see if the value ends with .exe (this way we can remove any command line arguments)
if ( !path.EndsWith( "exe" ) )
//get rid of all command line arguments (anything after the .exe must go)
path = path.Substring( 0, path.LastIndexOf( ".exe" ) + 4 );

initialized = true;
}
catch ( Exception ex )
{
ErrorMessage = string.Format( "ERROR: An exception of type: {0} occurred in method: {1} in the following module: {2}", ex.GetType(), ex.TargetSite, typeof( DefaultSystemBrowser ) );
}
finally
{
//check and see if the key is still open, if so
//then close it
if ( regKey != null )
regKey.Close();
}
}

}
}

Here is how I am using the code:

        DefaultSystemBrowser.Determine();
if ( DefaultSystemBrowser.ErrorMessage == null )
{
btnOpenInBrowser.Image = DefaultSystemBrowser.Bitmap;
}
else
{
btnOpenInBrowser.Image = Properties.Resources.firefox_24_noshadow;
}

How to set the default browser home page (IE) with C#?

Set it in this registry setting:

HKCU\Software\Microsoft\Internet Explorer\Main\Start Page


Related Topics



Leave a reply



Submit