Quantcast
Channel: Sleepiness in Seattle » search
Viewing all articles
Browse latest Browse all 2

Use Windows Desktop Search API Inside of Managed Code

$
0
0
Starting from Windows Desktop Search (WDS) 3.0, a new helper API ISearchQueryHelper is added to help simplify the construction of index OLE DB connection string and search queries. The search service is implemented and the API is exposed as COM objects. Fortunately, there is a way for managed code to invoke the WDS too.
Download the Windows Search SDK. After unzip the package, the search interop assembly (Microsoft.Search.Interop.dll) is in the "Managed" folder. Add it to the reference inside of your solution and put “using Microsoft.Search.Interop; ” at the top of your C# code. Now it’s ready to write C# code and make use of WDS API. Some of the unmanaged and managed counterparts of classes are the following:
Unmanaged
Managed
ISearchManager
CSearchManager
ISearchCatalogManager
CSearchCatalogManager
ISearchQueryHelper
CSearchQueryHelper
 
The following sample code constructs a search query using ISearchQueryHelper to display the sender, recipients, and summary of the first 100 emails that contain a particular keyword (“share” in this example).
int maxSumLength = 100;
 
// Setup the catalog and search query helper
CSearchManager srchMngr = new CSearchManager();
CSearchCatalogManager srchCatMngr = srchMngr.GetCatalog("SystemIndex");
CSearchQueryHelper srchQueryHelper = srchCatMngr.GetQueryHelper();
 
// Assemble the query
srchQueryHelper.QuerySelectColumns = "System.Message.FromAddress, System.Message.ToAddress, System.Search.AutoSummary";
srchQueryHelper.QueryWhereRestrictions = "AND CONTAINS(System.Kind, ‘\"email\"’)";
string sqlQuery = srchQueryHelper.GenerateSQLFromUserQuery("share");
 
// Setup the OLE DB connection
OleDbConnection conn = new OleDbConnection(srchQueryHelper.ConnectionString);
conn.Open();
 
// Execute the query
OleDbCommand cmd = new OleDbCommand(sqlQuery, conn);
OleDbDataReader srchResult = cmd.ExecuteReader();
 
// Process the search result and send to output
for (int i = 0; (i < 100) && srchResult.Read(); ++i)
{
                string fromAddr = "";
                string toAddr = "";
                string sumAddr = "";
 
                if (null != srchResult.GetValue(0) && !(srchResult.GetValue(0) is System.DBNull))
                {
                    string[] addrs = (string[])srchResult.GetValue(0);
                    fromAddr = "From: " + System.String.Join(",", addrs);
                }
                if (null != srchResult.GetValue(1) && !(srchResult.GetValue(1) is System.DBNull))
                {
                    string[] addrs = (string[])srchResult.GetValue(1);
                    toAddr = "To: " + System.String.Join(",", addrs);
                }
                if (null != srchResult.GetValue(2) && !(srchResult.GetValue(2) is System.DBNull))
                {
                    sumAddr = (string)srchResult.GetString(2);
                }
 
                textBox1.AppendText("(" + i + ") ");
                textBox1.AppendText(fromAddr + " " + toAddr + " ");
                textBox1.AppendText("Content: " + (sumAddr.Length <= maxSumLength ? sumAddr : sumAddr.Substring(0, maxSumLength)));
                textBox1.AppendText(Environment.NewLine);
}
 
srchResult.Close();
conn.Close();
           

Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images