-3

So I'm creating a program that can go into google and search for the word that has been saved into a variable. So my question is how I can do this, without having it go to a URL instead just going to Google and search "whatever" Here's the code.

    private void button2_Click(object sender, EventArgs e)
    {
        if (Script.Text == Script.Text)
        {
            Console.AppendText("\n[1] Loading Websites of " + Script.Text + "...");
        }
        WebClient wc = new WebClient();
        Process.Start(@"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", "Scripts " + Script.Text);
    }
  • WebClient wc = new WebClient(); is not required if you use Process.Start(). just put params in there as ( "Location of chrome.exe", "Your Target WebURL" ); for that – Arphile Jan 18 '19 at 04:28
  • I know that, but that is for a future statement that I will add in. Thank you for your help though! – CProgrammer Jan 18 '19 at 04:29
  • How about consuming [Google CustomSearch API](https://developers.google.com/api-client-library/dotnet/apis/customsearch/v1)....? – vikscool Jan 18 '19 at 04:29
  • 3
    Advices that explicitly violate TOC of sites are questionable... So I strongly recommend to perform your own research about "C# scrape google search" and read answers like https://stackoverflow.com/questions/9773724/scrape-googles-all-search-results-based-on-certain-criteria. (Which also allow you to demonstrate you own research in the question which currently lacks that information) – Alexei Levenkov Jan 18 '19 at 04:30
  • So, you want to open a web browser with that search term prefilled? If so, use `https://www.google.com?q=searchtermhere` and ensure that `searchtermhere` is url encoded. – mjwills Jan 18 '19 at 04:30
  • mjwills. What I want to do is for my program to open Google.exe and then automatically search for the site that is contained in my URL, so say my Variable is x = textBox1.Text Process.Start("BLAHBLAHBLAH/chrome.exe", x); – CProgrammer Jan 18 '19 at 04:31
  • Possible duplicate of [How to open in default browser in C#](https://stackoverflow.com/questions/4580263/how-to-open-in-default-browser-in-c-sharp) – mjwills Jan 18 '19 at 08:02

2 Answers2

1

This should solve your problem

public static void GoToSite(string url)
{
   System.Diagnostics.Process.Start("chrome.exe", url);
}

you can also change browser dynamically by adding a second params

nyulan
  • 311
  • 3
  • 12
0

You can also do this:

String stringToSearch = “stackoverflow”;

System.Diagnostics.Process.Start(@"C:\Program Files\Internet Explorer\iexplore.exe", "http://www.google.com.au/search?q=" + stringToSearch);

This assumes IE is the default browser on your machine. If Chrome or Firefox is the default browser, then code will fail.

This is a useful link:

http://www.seirer.net/blog/2014/6/10/solved-how-to-open-a-url-in-the-default-browser-in-csharp

Maybe you can put it in a try catch block and see if there is an exception. If there is an exception with starting IE, then open chrome.

Gauravsa
  • 6,330
  • 2
  • 21
  • 30
  • You can simply write iexplore.exe instead of the full path. And even though internet explorer is not the default browser, it still works! – preciousbetine Jan 18 '19 at 06:31