using System; using System.Data.OleDb; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Diagnostics; using System.Windows.Controls.Primitives; using System.Collections.ObjectModel; namespace RecipeMaker { class WindowsSearch { static string connectionString = @"Provider=Search.CollatorDSO;Extended Properties='Application=Windows'"; public static Collection PerformWindowsSearch(string searchtext) { Collection results = new Collection(); // do Windows Search string searchstring = searchtext; if (searchstring != "") { string Query; if (searchstring.ToLower() != "all") Query = "SELECT \"System.FileName\", \"System.Rating\", \"System.Keywords\", \"System.ItemAuthors\", \"System.ItemPathDisplay\" FROM SYSTEMINDEX..SCOPE() WHERE \"System.ItemType\" = '" + Helper.Extension + "' AND CONTAINS('" + searchstring + "')"; else Query = "SELECT \"System.FileName\", \"System.Rating\", \"System.Keywords\", \"System.ThumbnailCacheId\", \"System.ItemAuthors\", \"System.ItemPathDisplay\" FROM SYSTEMINDEX..SCOPE() WHERE \"System.ItemType\" = '" + Helper.Extension + "'"; using (OleDbConnection connection = new OleDbConnection(connectionString)) { try { connection.Open(); OleDbCommand cmd = new OleDbCommand(Query, connection); OleDbDataReader reader = cmd.ExecuteReader(); if (reader.HasRows) { int numColumns = reader.FieldCount; object[] values = new object[numColumns]; while (reader.Read()) { reader.GetValues(values); results.Add(GetRowTextValues(values)); } } } catch (Exception e) { Debug.WriteLine(">>>>>" + "PerformWindowsSearch: " + e.Message + ":" + e.StackTrace); Debug.WriteLine("---"); } } } return results; } private static string[] GetRowTextValues(object[] QueryResultValues) { // Process a row from the query results, and combine any multi-valued columns string[] textValues = new string[QueryResultValues.Length]; for (int i = 0; i < QueryResultValues.Length; i++) { if (!QueryResultValues[i].GetType().IsArray) { textValues[i] = QueryResultValues[i].ToString(); } else { int count = 0; string colValue = ""; foreach (object o in QueryResultValues[i] as Array) { if (count++ > 0) { colValue += ";"; } colValue += o.ToString(); } textValues[i] = colValue; } } return (textValues); } } }