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:
http://127.0.0.1:4664/ 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
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
To search for the two phrases "Google Desktop" and "Copyright 2007", use
&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:
The algorithm consists of:
- Getting Query URL from registry
- Parsing received XML to valid format
An example of XML result is shown below:
<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:
public class deserialize
public Result Results;
public class 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)
if (args.Length > 0)
RegistryKey currentUser = RegistryKey.OpenRemoteBaseKey
RegistryKey searchUrl = currentUser.OpenSubKey
object key = searchUrl.GetValue("search_url");
string query = "";
foreach (string var in args)
query += var + "+";
query = query.Remove(query.Length - 1);
string connection = key.ToString() + query + "&format=xml";
WebClient wc = new WebClient();
string result = wc.DownloadString(connection);
result = result.Insert(result.IndexOf("<results"), "<Added>");
result = result.Insert(result.Length - 1, "</Added>");
XmlSerializer ser = new XmlSerializer(typeof(deserialize));
System.IO.TextReader hehe = new System.IO.StringReader(result);
deserialize myObj = new deserialize();
myObj = (deserialize)ser.Deserialize(hehe);
And the printing method (You can experiment without
method to observe the difference.
method is used to delete any HTML tags from
private static void print(deserialize myObj)
System.Text.RegularExpressions.Regex asd =
foreach (Result var in myObj.Results)
(" --------------------------------------------------------- ");
Console.WriteLine("Title: " +
Console.WriteLine("Category: " + var.cat);
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