1

I found this link: C# Launch default browser with a default search query

But with FireFox as my default browser, it tries to find a file named as whatever is in the quotes in the selected Answer there.

Code;

    //ToolStripMenu Click event to launch default browser and search internet for the value in a particular cell
    private void tsmSearch_Click(object sender, EventArgs e)
    {
        int key = mp.GetRowAt(gdcErrorLogDefaultView, rowX, rowY);

        if (key < 0)
            return;

        string ex = gdcErrorLogDefaultView.GetRowCellValue(key, "Exception").ToString();

        string name = GetDefaultBrowser();

        Process.Start(name, "\"?" + ex + "\"");
    }

    //Gets default browser from registry
    private string GetDefaultBrowser()
    {
        string name;
        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 name;
    }

I found the GetDefaultBrowser() code somewhere on StackOverflow yesterday but I can't find the link now. The weird thing is though, I have Chrome set as my default browser but that registry key still says FireFox.

Is there a more simple way to launch the default browser and use their default search provider to look up a term?

Community
  • 1
  • 1
sab669
  • 3,984
  • 8
  • 38
  • 75
  • pre-windows 8 systems have no knowledge of a 'default search engine provider'. As a consequence, it is impossible to load the default browser with the default provider. What OS are you targeting? – Gusdor Nov 01 '13 at 13:16
  • @Gusdor Application will primarily be run on Windows 7, but some users have Vista or even XP. I doubt we'll see anything running 8 for a long time, if ever. – sab669 Nov 01 '13 at 13:16
  • 1
    then I'm afraid your request is not possible. You can detect the default browser but the method for getting that application to search with criteria will vary between browsers. http://stackoverflow.com/questions/2177957/how-to-determine-the-windows-default-browser-at-the-top-of-the-start-menu – Gusdor Nov 01 '13 at 13:18
  • I should add that firefox uses this command line for http requests. `"C:\Program Files (x86)\Mozilla Firefox\firefox.exe" -osint -url "%1"`. You can see the `-url "%1"` switch taking argument 1 from the command line. The command line for Google Chrome lets you append the URL directly, with no `-url` switch, but (!!!)... Firefox may behave this way as well. Internet Explorer? Who knows. It is all undefined. – Gusdor Nov 01 '13 at 13:28
  • Hmm, if I'm particularly bored maybe I'll just write a switch case for the three browsers and try to do the appropriate Process then? – sab669 Nov 01 '13 at 13:34
  • you could give it a shot but if the browser isn't on the list, it may not work at all. Your best bet is just to chuck your google url in as the first commandline and see if that works. It does in firefox :D `C:\Program Files (x86)\Mozilla Firefox\firefox.exe "google.co.uk/search?q=who+needs+bing"` – Gusdor Nov 01 '13 at 13:35

1 Answers1

1

Try this:

string searchQuery = "this is a search";
Process.Start("https://www.google.com/search?q=" + Uri.EscapeDataString(searchQuery));

Edit: Now using correct Google link

Codecat
  • 2,213
  • 3
  • 28
  • 39