4

What I need to do is launch the default browser with a default search. The default search is what happens when you type search terms in the URL navigation text box. For example, in Chrome and Firefox, by default, typing "puppies" into the nav text box will lead you to the Google results for "puppies". In IE, it will do the same thing, just on Bing.

Normally, you can invoke the default browser just by doing something like:

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

But I can't assume the default search provider is Google.

Is there a way to invoke this behavior via C#? The only thing I can figure to do is to try and determine which browser is the default and then execute it directly with the search terms.

Anyone know of any other (preferably easier) way?

Update: Just found code to find the default browser here.

Community
  • 1
  • 1
jpreed00
  • 893
  • 8
  • 25
  • have you tried to start process with link having the query? For example `Process.Start("https://www.google.co.uk/#sclient=psy-ab&q=my+search+query")` – alex.b May 16 '13 at 16:11
  • @aleksey.berezan This presumes google is default provider. The issue here is that can either start default browser to a site, or start a search with a specific browser, he needs a way to start default browser and use default search provider. – Nate May 16 '13 at 16:13

2 Answers2

2

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.

Dominic P
  • 2,841
  • 1
  • 14
  • 16
  • This works perfectly in conjunction with the code for finding the default browser. Thanks! – jpreed00 May 16 '13 at 21:14
  • @DominicP Don't mind to dredge up an extremely old post of yours, but any this doesn't seem to work with FireFox or Chrome v26.0.1410.64 m... Any idea what the arguments are for those browsers? – sab669 Nov 01 '13 at 14:04
  • @sab669 The version of Chrome that I have is 30.0.1599.101 m, and it works for me there. Find where chrome.exe exists in your filesystem (probably ...\AppData\Local\Google\Chrome\Application\chrome.exe under your user profile), navigate there at command prompt, and try typing `chrome.exe "? abalone"`. Does it not open up chrome with search results? – Dominic P Nov 01 '13 at 20:58
  • @DominicP oh, I forgot the space. It works without the space in IE, but not chrome. – sab669 Nov 04 '13 at 16:56
1

I just tested IE, by typing this in to the Start -> Run prompt:

"c:\Program Files\Internet Explorer\iexplore.exe" "stack overflow"

It fired up Internet Explorer and searched for stack overflow with my default search provider. Since your search terms are not a standard url starting with http:// there is no way for Process.Start to know to fire up the browser if you don't provide the specific executable you want started.

You can identify the default browser by inspecting HKEY_CLASSES_ROOT\http\shell\open\command Then you can fire up this browser with the search terms as a query parameter and it should then use that browsers default search provider.

Nate
  • 30,286
  • 23
  • 113
  • 184
  • Yup, I just found the code for retrieving the default browser elsewhere on StackOverflow, I'll update my post. This technique does not appear to work for Chrome, though. – jpreed00 May 16 '13 at 16:16
  • Must be something to do with the search term itself. "c:\Program Files\Internet Explorer\iexplore.exe" "stack overflow" does return search results. "c:\Program Files\Internet Explorer\iexplore.exe" "puppies" returns an "Internet Explorer cannot display the webpage" error after trying to retrieve http://puppies/. – jpreed00 May 16 '13 at 16:21
  • 1
    @jpreed00 That's because of the space between "stack" and "overflow". As it's invalid in a URL, IE understood you want to search it using the default search provider. – ken2k May 16 '13 at 16:44