Click here to Skip to main content
12,892,320 members (49,350 online)
Click here to Skip to main content
Add your own
alternative version


29 bookmarked
Posted 14 Sep 2007

Google Desktop Command Line Search

, 14 Sep 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
How to perform Google desktop search query in C#


One day I discovered that it would be nice to have a search engine when connecting to your computer via SSH. In this article, I will describe how easy it is to write an application in C# language performing desktop search.


The goal is to use Google Desktop API to perform search queries. As it is described in Google Desktop Query API Developer Guide, there are few ways of using GD engine. The easiest of them is HTML/XML - based Query API. The only thing you need to know is how to deal with HTTP Request and XML processing in .NET. We will send the query over HTTP protocol and receive search results in XML format.

Requesting a Desktop Search

Example query (from Google desktop API guide):

Google description of the above query is shown below:

  • is the localhost address and Google Desktop port.
  • search&s=1ftR7c_hVZKYvuYS-RWnFHk91Z0 is the search command and a security token.
  • ?q=Google is the query term(s) parameter.
    • If you want to search for more than one term, separate the terms with +s. For example, to search for both "Google" and "Desktop", use ?q=Google+Desktop
    • If you want to search for a specific phrase, separate the terms with +s and surround the phrase with %22s. For example, to search for the phrase "Google Desktop", use ?q=%22Google+Desktop%22
      To search for the two phrases "Google Desktop" and "Copyright 2007", use ?q=%22Google+Desktop%22+%22Copyright+2007%22
  • &format=xml specifies that the HTTP response returns the search results in XML format, as described in the next section.

Query URL is kept in Windows registry:

HKEY_CURRENT_USER\Software\Google\Google Desktop\API\search_url

The algorithm consists of:

  • Getting Query URL from registry
  • Performing HttpWebRequest
  • Parsing received XML to valid format

An example of XML result is shown below:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<results count="24945">
  <!-- ... optional internal, 
	implementation-specific elements such as ids and flags... --> 
  <title>Developer Knowledge Base - Read the Google Desktop blog</title> 
  <snippet>Desktop engineers regularly post development articles and 
	announce updates to th...</snippet> 



In this example, I will use only title, URL, and category nodes. Here is the class example used to deserialize the XML:

namespace googleDesktop
    public class deserialize
        public Result[] Results;

        public deserialize()

    public class Result
        public Result()

        public string location;

        public string cat;

        public string title;

As you can see, our XML <results></results> content should be enclosed in another node. That's why we will add the <Added></Added> nodes in the proper place. Now you are ready to analyze the code:

static void Main(string[] args)
            // Search keywords are derived in command line arguments
            if (args.Length > 0)
                // Obtain Query URL from Windows registry
                RegistryKey currentUser = RegistryKey.OpenRemoteBaseKey
				(Microsoft.Win32.RegistryHive.CurrentUser, "");
                RegistryKey searchUrl = currentUser.OpenSubKey
				("Software\\Google\\Google Desktop\\API");
                object key = searchUrl.GetValue("search_url");
                string query = "";

                // Add search keywords to query URL according to scheme - 
                // keywords separated by '+'
                foreach (string var in args)
                    query += var + "+";

                query = query.Remove(query.Length - 1);

                // Last element of the query
                string connection = key.ToString() + query + "&format=xml";

                // Create Web Client
                WebClient wc = new WebClient();

                // Obtain XML as string
                string result = wc.DownloadString(connection);

                // Insert <Added> node before the <results>
                result = result.Insert(result.IndexOf("<results"), "<Added>");
                // Insert </Added> after the </results>
                result = result.Insert(result.Length - 1, "</Added>");

                // Prepare serializer...
                XmlSerializer ser = new XmlSerializer(typeof(deserialize));
                // ... and textreader
                System.IO.TextReader hehe = new System.IO.StringReader(result);

                // Create instance of 'deserialize' class
                deserialize myObj = new deserialize();

                // Deserialize
                myObj = (deserialize)ser.Deserialize(hehe);

                // Print the results
And the printing method (You can experiment without HtmlDecode method to observe the difference. Regex method is used to delete any HTML tags from string):
private static void print(deserialize myObj)
            System.Text.RegularExpressions.Regex asd = 
		new System.Text.RegularExpressions.Regex("<[^>]*>");
            foreach (Result var in myObj.Results)
		(" --------------------------------------------------------- ");
                Console.WriteLine("Title: " + 
		System.Web.HttpUtility.HtmlDecode(asd.Replace(var.title, "")));
                Console.WriteLine("Category: " +;
                Console.WriteLine("Location: " + 


The purpose of this article was to show you only the idea of using Google Desktop engine. Google Desktop API has more features that I've omitted. The code is obviously not error proof. If someone will find a way to construct 'deserialize' class without adding additional nodes, please post the solution. I'd love to see that.


  • 15th September, 2007: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

Generalthank you very much! Pin
Member 265515625-Nov-08 12:30
memberMember 265515625-Nov-08 12:30 
QuestionHTTP Request works but not for Google Desktop Request Pin
harau24-Nov-07 23:18
memberharau24-Nov-07 23:18 
AnswerRe: HTTP Request works but not for Google Desktop Request Pin
piotr.kolodziej25-Nov-07 2:48
memberpiotr.kolodziej25-Nov-07 2:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 15 Sep 2007
Article Copyright 2007 by piotr.kolodziej
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid