1

I want to open default browser in my computer and then simulate to click the button. I have created a new console application as below:

class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        var url = "http://google.com";
        Process.Start(url);
        Console.ReadKey();
    }
}

Now what can I do? I found WebBrowser class but I think it's for browser control in windows forms app..

Balagurunathan Marimuthu
  • 2,927
  • 4
  • 31
  • 44
gsiradze
  • 4,583
  • 15
  • 64
  • 111

1 Answers1

1

Get default browser using the below function

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

Open url in default browser:

string url = "http://google.com";
string browserPath = GetStandardBrowserPath();
if (string.IsNullOrEmpty(browserPath))
{
    MessageBox.Show("No default browser found!");
}
else
{
    Process.Start(browserPath, url);
}
Balagurunathan Marimuthu
  • 2,927
  • 4
  • 31
  • 44