I am looking for a code that gets results of full text search using Windows search (it should be available in Vista, 7 and 8 by default).
I have found some questions here and some texts on msdn, but none of them have some exact code that works. I have tried with Windows API Code Pack (as it is mentioned as one of the interfaces to Windows Search), but it returns results only for file names, not for full text.
Turn on Option To Search Through File ContentsOn the Indexing Options dialog box, click Advanced. Click the File Types tab on the Advanced Options dialog box. By default, all the extensions are selected, and that's what we want. This will allow Windows to search through all the types of files on your hard drive.
Tap on the Windows start button and open “Settings.” Click on “Search,” then “Searching Windows.” Under “Classic,” select “Customize search location here.”
If you'd like to always search within file contents for a specific folder, navigate to that folder in File Explorer and open the “Folder and Search Options.” On the “Search” tab, select the “Always search file names and contents” option.
Here is the code that does work - in example I made it to search for the word "dummy" in the desktop folder:
string connectionString = "Provider=Search.CollatorDSO;Extended Properties=\"Application=Windows\"";
OleDbConnection connection = new OleDbConnection(connectionString);
string query = @"SELECT System.ItemName FROM SystemIndex " +
@"WHERE scope ='file:" + System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "' and FREETEXT('dummy')";
OleDbCommand command = new OleDbCommand(query, connection);
connection.Open();
List<string> result = new List<string>();
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
result.Add(reader.GetString(0));
}
connection.Close();
Take a look at the DSearch example. Windows Search Code Samples
That's what you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With