How can I perform search through files like queries to Windows Search platform using .net-core?

218 Views Asked by At

I need to search files by file name and content. Current implementation used Ole DB connection to windows search. But as I understand, Ole db wouldn't be implemented in .net core. I guess that I should use solution like Lucene .So I need advice, how to access windows search from .net-core at least or any ideas how to make that in cross-platform manner without windows search.

1

There are 1 best solutions below

2
On

You basically have a couple of options. You can get hold of the FTQuery Tool (FTQuery.exe) in Windows 7 SDK and use Process Class in System.Diagnostics to get results by parsing standard output.

Alternatively you can create a separate ASP.NET Web API 2 project to act as a bridge between Windows Search and .NET Core using the following code as an action in your controller.

public async Task<IHttpActionResult> Post()
{
    var data = new ArrayList();

    var conn = new OleDbConnection("Provider=Search.CollatorDSO;Extended Properties='Application=Windows'");
    await conn.OpenAsync();

    string sql = await Request.Content.ReadAsStringAsync();
    var cmd = new OleDbCommand(sql, conn);

    using (var rdr = await cmd.ExecuteReaderAsync(CommandBehavior.CloseConnection))
    {
        while (await rdr.ReadAsync())
        {
            var row = new Dictionary<string, object>();
            for (int i = 0; i < rdr.FieldCount; i++)
            {
                if (!rdr.IsDBNull(i))
                    row.Add(rdr.GetName(i), rdr.GetValue(i));
            }
            data.Add(row);
        }
    }

    return Json(data);
}