I am trying to write a WinForms app, that allows searching for a files, that contain a string written in textbox ( press WIN+F and you understand ;) ) in this app, there is a list of files and directories, that must be searched for this string
those files are mostly .doc and .xls i think, that searching in doc may be easier, but in Excel files, cells can have different encodings i've tried to "read" those files, by opening them in Notepad++, and i found that cells with only latin characters were easy to find, but those with polish characters, had a two byte encoding
in windows built in search, there was no problem, it was able to tell, that in some test files there is my string that contains polish special characters
so my question basically is, if there is a method to use this windows built-in search engine for my app ( as i wrote, i need to find only filenames ), or maybe you have 开发者_StackOverflow中文版any other idea, how i can write a simple multi-file search ?
You probably want to use the Windows Search SDK.
You can interact with windows search in your code, allowing it to do the heavy lifting of searching multiple file types. See this MSDN article for more information:
http://msdn.microsoft.com/en-us/library/bb266517%28v=VS.85%29.aspx
Check out this website on using the Windows Indexing APIs. It refers to ASP.NET but the code is in C#.
Snippet below:
string QueryText = "asp alliance"; //The search string
string CatalogName = "searchcatalog"; //The name of your Index Server catalog
int NumberOfSearchResults = 0;
DataSet SearchResults = new DataSet();
//Prevent SQL injection attacks - further security measures are recommended
QueryText = QueryText.Replace("'", "''");
//Build the search query
string SQL = "SELECT doctitle, vpath, Path, Write, Size, Rank ";
SQL += "FROM \"" + CatalogName + "\"..SCOPE() ";
SQL += "WHERE";
SQL += " CONTAINS(Contents, '" + QueryText + "') ";
SQL += "AND NOT CONTAINS(Path, '\"_vti_\"') ";
SQL += "AND NOT CONTAINS(FileName, '\".ascx\" OR \".config\" OR \".css\"') ";
SQL += "ORDER BY Rank DESC";
//Connect to Index Server and perform search query
try
{
OleDbConnection IndexServerConnection = new OleDbConnection("Provider=msidxs;");
OleDbCommand dbCommand = new OleDbCommand(SQL, IndexServerConnection);
OleDbDataAdapter IndexServerDataAdapter = new OleDbDataAdapter();
IndexServerDataAdapter.SelectCommand = dbCommand;
IndexServerDataAdapter.Fill(SearchResults);
NumberOfSearchResults = SearchResults.Tables[0].Rows.Count;
}
catch (Exception ExceptionObject)
{
//Query failed so display an error message
NumberOfSearchResults = 0;
LabelNumberOfResults.Text = "Problem with retrieving search results due to: " + ExceptionObject.Message;
DataGridSearchResults.Visible = false;
}
精彩评论