Launch Google Search in default Browser


/ Published in: C#
Save to your folder(s)

Properly escapes the query string, according to http://code.google.com/apis/searchappliance/documentation/46/xml_reference.html#appendix_url_escaping


Copy this code and paste it in your HTML
  1. /// <summary>
  2. /// Class to launch Google Search in default browser.
  3. /// </summary>
  4. public static class Google {
  5. /// <summary>
  6. /// Launches Google Search in default browser, and escapes string according to: http://code.google.com/apis/searchappliance/documentation/46/xml_reference.html#appendix_url_escaping
  7. /// </summary>
  8. /// <param name="searchQuery">The search query.</param>
  9. public static void SearchGoogle(string searchQuery) {
  10. string fixedSearchQuery = null;
  11.  
  12. foreach (char character in searchQuery) {
  13. if (Char.IsLetterOrDigit(character)) {
  14. fixedSearchQuery += character;
  15. } else if (character == Char.Parse(" ")) {
  16. fixedSearchQuery += "+";
  17. } else {
  18. fixedSearchQuery += Uri.HexEscape(character);
  19. }
  20. }
  21.  
  22. string url = @"http://www.google.com/search?hl=en&q=" + fixedSearchQuery;
  23.  
  24. try {
  25. Process.Start(url);
  26. } catch { }
  27. }
  28. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.