I have a requirement to search file in a given location for specified content. These files will be searched via a web-application (which may not necessarily be on the same server), and should update in near realtime.
After looking around for Scanning tools/code, I hit upon this answer that indicated that you can programmatically hook into the built in Windows Search feature of windows.
Using the code below (more or less the answers code with a few minor tweaks), I have been able to successfully make this work on my machine:
public class WindowsSearchScanner
{
private const string Query = @"SELECT System.ItemName FROM SystemIndex WHERE scope ='file:{0}' and FREETEXT('{1}')";
public void DoSearch(string criteria, string path)
{
string connectionString = "Provider=Search.CollatorDSO;Extended Properties=\"Application=Windows\"";
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
string query = string.Format(Query, path, criteria);
OleDbCommand command = new OleDbCommand(query, connection);
connection.Open();
List<string> result = new List<string>();
OleDbDataReader reader = command.ExecuteReader();
int count = 0;
while (reader.Read())
{
result.Add(reader.GetString(0));
Console.WriteLine(reader.GetString(0));
count++;
}
Console.WriteLine(count + " Records Found");
connection.Close();
}
}
}
I have several queries regarding this:
Is there a way to parameterise the text? I tried straight up adding OleDbParameters
to the command, but apparently the CollatorDSO doesn't support it. Obviously, I'd prefer not to have to sanitise the data beforehand - like SQL injection, it's likely I'll miss some potential avenue that will cause problems
string query = @"SELECT System.ItemName FROM SystemIndex WHERE scope ='file:' + @path and FREETEXT(@text)";
OleDbCommand command = new OleDbCommand(query, connection);
command.Parameters.Add(new OleDbParameter("path", path));
command.Parameters.Add(new OleDbParameter("text", criteria));
connection.Open();
Can this be accessed from a remote machine?
for 3) ,4) msdn Using SQL and AQS Approaches to Query the Index for 1) you can try add windows function via import unsafe. for 2) looks wierd old fashioned but i suppose there is no other option for this approach
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