public static void InitQueryHelper(out ISearchQueryHelper queryHelper, int maxCount) { // This uses the Microsoft.Search.Interop assembly CSearchManager manager = new CSearchManager(); // SystemIndex catalog is the default catalog in Windows ISearchCatalogManager catalogManager = manager.GetCatalog("SystemIndex"); // Get the ISearchQueryHelper which will help us to translate AQS --> SQL necessary to query the indexer queryHelper = catalogManager.GetQueryHelper(); // Set the number of results we want. Don't set this property if all results are needed. queryHelper.QueryMaxResults = maxCount; // Set list of columns we want to display, getting the path presently queryHelper.QuerySelectColumns = "System.ItemUrl, System.FileName, System.FileAttributes"; // Set additional query restriction queryHelper.QueryWhereRestrictions = "AND scope='file:'"; // To filter based on title for now queryHelper.QueryContentProperties = "System.FileName"; // Set sorting order queryHelper.QuerySorting = "System.DateModified DESC"; }
public static void InitQueryHelper(out ISearchQueryHelper queryHelper, ISearchManager manager, int maxCount, bool displayHiddenFiles) { if (manager == null) { throw new ArgumentNullException(nameof(manager)); } // SystemIndex catalog is the default catalog in Windows ISearchCatalogManager catalogManager = manager.GetCatalog("SystemIndex"); // Get the ISearchQueryHelper which will help us to translate AQS --> SQL necessary to query the indexer queryHelper = catalogManager.GetQueryHelper(); // Set the number of results we want. Don't set this property if all results are needed. queryHelper.QueryMaxResults = maxCount; // Set list of columns we want to display, getting the path presently queryHelper.QuerySelectColumns = "System.ItemUrl, System.FileName, System.FileAttributes"; // Set additional query restriction queryHelper.QueryWhereRestrictions = "AND scope='file:'"; if (!displayHiddenFiles) { // https://docs.microsoft.com/en-us/windows/win32/search/all-bitwise queryHelper.QueryWhereRestrictions += " AND System.FileAttributes <> SOME BITWISE " + _fileAttributeHidden; } // To filter based on title for now queryHelper.QueryContentProperties = "System.FileName"; // Set sorting order queryHelper.QuerySorting = "System.DateModified DESC"; }
static void Main(string[] args) { Application objApp = null; ISearchManager objManager = null; ISearchCatalogManager objCatalog = null; ISearchCrawlScopeManager objScope = null; string strSID = null; try { Console.WriteLine("Creating search management objects..."); objManager = new CSearchManagerClass(); objCatalog = objManager.GetCatalog("SystemIndex"); objScope = objCatalog.GetCrawlScopeManager(); Console.WriteLine("Obtaining currently logged user's SID..."); strSID = String.Concat("{", WindowsIdentity.GetCurrent().User.Value.ToString().ToLower(), "}"); Console.WriteLine(" The SID is: {0}", strSID); Console.WriteLine("Starting Outlook application..."); objApp = new Application(); Console.WriteLine("Enumerating PST files..."); foreach (Store objStore in objApp.GetNamespace("MAPI").Stores) { Console.WriteLine("Analysing file: {0}...", objStore.FilePath); if (objStore.ExchangeStoreType != OlExchangeStoreType.olNotExchange || objStore.IsCachedExchange != true) { Console.WriteLine(" Rejected. This file is not cached exchange store."); } else if (Path.GetPathRoot(objStore.FilePath).StartsWith("C", StringComparison.OrdinalIgnoreCase)) { Console.WriteLine(" Rejected. This file is located on the C: drive."); } else if (objStore.IsInstantSearchEnabled != true) { Console.WriteLine(" Rejected. Instant search was already disabled for this file."); } else { Console.WriteLine(" Accepted. Indexing of this file will be disabled."); Console.WriteLine(" Computing store hash..."); string strHash = ComputeHash(objStore.StoreID); Console.WriteLine(" The hash is: {0}.", strHash); string strUrl = String.Format("mapi://{0}/{1}(${2})/", strSID, objStore.DisplayName, strHash); Console.WriteLine(" Disabling indexing..."); Console.WriteLine(" The rule url is: {0}.", strUrl); objScope.AddUserScopeRule(strUrl, 0, 1, 0); objScope.SaveAll(); Console.WriteLine(" Operation successfull!"); } } } catch (System.Exception e) { Console.WriteLine(); Console.WriteLine("An error occured!"); Console.WriteLine(e.ToString()); } Console.WriteLine(); Console.WriteLine("Press return to exit."); Console.ReadLine(); }
private void btnSearch_Click(object sender, EventArgs e) { //const string connectionString = "Provider=Search.CollatorDSO; Extended Properties=\"Application=Windows\""; //using (OleDbConnection connection = new OleDbConnection(connectionString)) //{ // // Список поддерживаемых свойств (обширный!) доступен на MSDN // // https://msdn.microsoft.com/en-us/library/ff521735%28v=vs.85%29.aspx // string query = @"SELECT System.ItemName, System.ItemFolderPathDisplayNarrow FROM SystemIndex " + // @"WHERE scope ='file\u003a" + GetSearchPath() + "'"; //" AND FREETEXT('XML')"; // 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(reader.GetOrdinal("System.ItemName"))); // listBox1.Items.Add(reader.GetString(reader.GetOrdinal("System.ItemName"))); // } // //result.Dump(); //} CSearchManager manager = new CSearchManagerClass(); ISearchCatalogManager catalogManager = manager.GetCatalog("SystemIndex"); //catalogManager. //ISearchQueryHelper queryHelper = catalogManager.GetQueryHelper(); //queryHelper. //string sqlQuery = queryHelper.GenerateSQLFromUserQuery(aqsQuery); }
static void Main(string[] args) { if (args.Length < 1) { Console.WriteLine("Usage: ds [file search pattern] [userQuery]"); Console.WriteLine("[file search pattern] can include '*' and '?' wildcards"); Console.WriteLine("[userQuery] query to look for inside the document (with operators of OR, AND). Optional."); return; } // First parameter is file name pattern string pattern = args[0]; string userQuery = " "; // Everything else is considered to be a search query for (int i = 1; i < args.Length; i++) { userQuery += args[i] + " "; } // This uses the Microsoft.Search.Interop assembly CSearchManager manager = new CSearchManager(); // SystemIndex catalog is the default catalog in Windows ISearchCatalogManager catalogManager = manager.GetCatalog("SystemIndex"); // Get the ISearchQueryHelper which will help us to translate AQS --> SQL necessary to query the indexer ISearchQueryHelper queryHelper = catalogManager.GetQueryHelper(); // Set the number of results we want. Don't set this property if all results are needed. queryHelper.QueryMaxResults = 10; // Set list of columns we want queryHelper.QuerySelectColumns = "System.ItemPathDisplay"; // Set additional query restriction queryHelper.QueryWhereRestrictions = "AND scope='file:'"; // convert file pattern if it is not '*'. Don't create restriction for '*' as it includes all files. if (pattern != "*") { pattern = pattern.Replace("*", "%"); pattern = pattern.Replace("?", "_"); if (pattern.Contains("%") || pattern.Contains("_")) { queryHelper.QueryWhereRestrictions += " AND System.FileName LIKE '" + pattern + "' "; } else { // if there are no wildcards we can use a contains which is much faster as it uses the index queryHelper.QueryWhereRestrictions += " AND Contains(System.FileName, '" + pattern + "') "; } } // Set sorting order queryHelper.QuerySorting = "System.DateModified DESC"; // Generate SQL from our parameters, converting the userQuery from AQS->WHERE clause string sqlQuery = queryHelper.GenerateSQLFromUserQuery(userQuery); Console.WriteLine(sqlQuery); // --- Perform the query --- // create an OleDbConnection object which connects to the indexer provider with the windows application using (System.Data.OleDb.OleDbConnection conn = new OleDbConnection(queryHelper.ConnectionString)) { // open the connection conn.Open(); // now create an OleDB command object with the query we built above and the connection we just opened. using (OleDbCommand command = new OleDbCommand(sqlQuery, conn)) { // execute the command, which returns the results as an OleDbDataReader. using (OleDbDataReader WDSResults = command.ExecuteReader()) { while (WDSResults.Read()) { // col 0 is our path in display format Console.WriteLine("{0}", WDSResults.GetString(0)); } } } } }
private IEnumerable <string> FindFiles(string pattern) { string userQuery = " "; // This uses the Microsoft.Search.Interop assembly CSearchManager manager = new CSearchManager(); // SystemIndex catalog is the default catalog in Windows ISearchCatalogManager catalogManager = manager.GetCatalog("SystemIndex"); // Get the ISearchQueryHelper which will help us to translate AQS --> SQL necessary to query the indexer ISearchQueryHelper queryHelper = catalogManager.GetQueryHelper(); // Set the number of results we want. Don't set this property if all results are needed. queryHelper.QueryMaxResults = 10; // Set list of columns we want queryHelper.QuerySelectColumns = "System.ItemUrl"; // Set additional query restriction queryHelper.QueryWhereRestrictions = "AND scope='file:'"; // convert file pattern if it is not '*'. Don't create restriction for '*' as it includes all files. if (pattern != "*") { pattern = pattern.Replace("*", "%"); pattern = pattern.Replace("?", "_"); if (pattern.Contains("%") || pattern.Contains("_")) { queryHelper.QueryWhereRestrictions += " AND System.FileName LIKE '" + pattern + "' "; } else { // if there are no wildcards we can use a contains which is much faster as it uses the index queryHelper.QueryWhereRestrictions += " AND Contains(System.FileName, '" + pattern + "') "; } } // Set sorting order queryHelper.QuerySorting = "System.DateModified DESC"; // Generate SQL from our parameters, converting the userQuery from AQS->WHERE clause string sqlQuery = queryHelper.GenerateSQLFromUserQuery(userQuery); Console.WriteLine(sqlQuery); // --- Perform the query --- // create an OleDbConnection object which connects to the indexer provider with the windows application using (var conn = new OleDbConnection(queryHelper.ConnectionString)) { // open the connection conn.Open(); // now create an OleDB command object with the query we built above and the connection we just opened. using (var command = new OleDbCommand(sqlQuery, conn)) { // execute the command, which returns the results as an OleDbDataReader. using (var results = command.ExecuteReader()) { while (results.Read()) { var url = results.GetString(0); if (url.StartsWith("file:", StringComparison.InvariantCultureIgnoreCase)) { yield return(url.Substring(5).Replace('/', '\\')); } } } } } }