Introduction
Another great service provided by Windows Vista is its integrated search. Can we use it from our WPF application? Sure we can. This is how you'll do it.
Background
First of all, you'll need to find Windows Search
API library inside Windows SDK. Locate SearchAPI.tlb and process it with tlbimp tool to create managed assembly to reference to. You'll get a file, named SeachAPILib.dll. This one, you will need. In order to use it, refer to MSDN documentation of Windows Search API 3.0. Actually, using the Search
API is not much different than using database queries. However, this is not so trivial in core. Actually, the syntax of Windows Search (AQS - Advanced Query Syntax) is very different. But the search team created brilliant work to make our life easier. CSearchManager
and ISeachQueryHelper
- those translate AQS to SQL and, even provide us with a proper connection string
.
Using the Code
First of all, we'll have to create CSeachManager
:
CSearchManager cManager = new CSearchManagerClass();
Then, while you are in query, you'll create new ISearchQueryHelper
to help you with translations and query string
s:
ISearchQueryHelper cHelper = cManager.GetCatalog("SYSTEMINDEX").GetQueryHelper();
cHelper.QuerySelectColumns = "\"System.ItemNameDisplay\"";
Well done, now. Ask Vista with old good OleDB provider:
using (cConnection = new OleDbConnection(cHelper.ConnectionString))
{
cConnection.Open();
using (OleDbCommand cmd = new OleDbCommand(
cHelper.GenerateSQLFromUserQuery(SearchString),cConnection))
{
if (cConnection.State == ConnectionState.Open)
{
using (OleDbDataReader reader = cmd.ExecuteReader())
{
m_results.Clear();
while (!reader.IsClosed && reader.Read())
{
m_results.Add(reader[0].ToString());
}
reader.Close();
}
}
}
cConnection.Close();
}
Brilliant. Let's move it into WPF by creating DependencyObject
, that provides us with all we need for search - search string and array of results:
public static readonly DependencyProperty SearchTextProperty =
DependencyProperty.Register
("SearchText", typeof(string), typeof(VistaSearchProviderHelper),
new UIPropertyMetadata(default(string),
new PropertyChangedCallback(OnSearchTextChanged)));
static void OnSearchTextChanged
(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
ToAbortFlag = IsWorkingFlag;
if (workerCallback == null) workerCallback = new WaitCallback(doSearch);
searchObjState.SearchString = e.NewValue.ToString();
{
workerCallback.BeginInvoke(searchObjState, null, null);
}
}
public string SearchText
{
get { return GetValue(SearchTextProperty).ToString(); }
set { SetValue(SearchTextProperty, value); }
}
static ThreadSafeObservableCollection<string> m_results;
public ReadOnlyObservableCollection<string> Results
{
get { return new ReadOnlyObservableCollection<string>(m_results); }
}
This code uses a custom class named ThreadSafeObservableCollection
, introduced earlier in my blog.
The only thing we should do now is to bind input and output to the XAML presentation:
<StackPanel FocusManager.FocusedElement="{Binding ElementName=searchStr}">
<StackPanel.DataContext>
<ObjectDataProvider ObjectType="l:VistaSearchProviderHelper"/>
</StackPanel.DataContext>
<TextBox Name="searchStr" Text="{Binding Path=SearchText,
UpdateSourceTrigger=PropertyChanged }" FontSize="30"/>
<ListBox ItemsSource="{Binding Path=Results}" />
</StackPanel>
Conclusion
Now we can search our Windows Vista from a custom WPF application. This is not the final application for sure. There is a long way to go to make it work perfectly, but this is the beginning of how to do it.
History
- 1st November, 2007: Initial post