How can I programmatically use Windows File Explorer to perform a Search?

你离开我真会死。 提交于 2019-12-08 04:36:18

问题


We have a folder on our network that we want to search within, including subfolders, from our program. We want to return a list of files whose name contains "String1" or "String2" or "StringN". We would prefer to programmatically open an Explorer window and view all files that match the search results using the native windows file explorer.

Is this possible? How?

Thanks!


回答1:


You can use the .ms-search file format to express a saved search. If you open this file format it will launch a File Explorer with the search conditions applied.

If you already have a File Explorer window opened with a search applied, you can save that using the 'save search' button on the ribbon.




回答2:


Since the Windows Explorer changes with every version of the OS ... I'd recommend doing the search via .NET and displaying the results on a grid on a form.

Use the System.IO namespace.

System.IO.Directory.GetFiles(folderName) will get the files ...

You will need a recursive function to enumerate all of the files first, then recurse each of the subdirectories.




回答3:


Here is some code to create the saved search xml file (file.search-ms) for searches by name with specified path and search string:

/// <summary>
    /// Generates the XDocument needed to create a .search-ms file for the path and mask given where mask specifies name parameter.
    /// search string in Windows is name:~searchmask. Searchmask includes * at beginning and end to find string present anywhere in name
    /// 
    /// </summary>
    /// <param name="searchPath">absolute path eg C:\Pictures\</param>
    /// <param name="searchMask"> part of search string after name:~</param>
    /// <returns></returns>
    public XDocument GenerateSearchDocName(string searchPath, string searchMask)
    {
        //  "*[*2602_Australia_Australian Capital Territory_O'Connor*].*" - example searchMask

        string str = @"<?xml version=""1.0""?><persistedQuery version=""1.0""><viewInfo viewMode=""icons"" iconSize=""96"" stackIconSize=""0"" displayName=""Search Results in iPhoneSample"" autoListFlags=""0""><visibleColumns><column viewField=""System.ItemNameDisplay""/><column viewField=""System.ItemDate""/><column viewField=""System.Keywords""/><column viewField=""System.Size""/><column viewField=""System.Rating""/><column viewField=""System.ItemFolderPathDisplay""/></visibleColumns><sortList><sort viewField=""System.Search.Rank"" direction=""descending""/><sort viewField=""System.ItemDate"" direction=""descending""/><sort viewField=""System.ItemNameDisplay"" direction=""ascending""/></sortList></viewInfo><query><conditions><condition type=""leafCondition"" property=""System.ItemNameDisplay"" operator=""matches"" propertyType=""string"" ";

        str += @"value=" + searchMask ;
        str+= @" localeName=""en-US""><attributes><attribute attributeID=""{9554087B-CEB6-45AB-99FF-50E8428E860D}"" clsid=""{C64B9B66-E53D-4C56-B9AE-FEDE4EE95DB1}"" chs=""1"" sqro=""585"" timestamp_low=""3078723010"" timestamp_high=""30601338""><condition type=""leafCondition"" property=""System.ItemNameDisplay"" operator=""matches"" propertyType=""string"" ";

        str += @"value=" + searchMask;
        str+= @" localeName=""en-US""><attributes><attribute attributeID=""{9554087B-CEB6-45AB-99FF-50E8428E860D}"" clsid=""{C64B9B66-E53D-4C56-B9AE-FEDE4EE95DB1}"" chs=""1"" sqro=""585"" timestamp_low=""2194097220"" timestamp_high=""30601338""><condition type=""leafCondition"" property=""System.ItemNameDisplay"" operator=""matches"" propertyType=""string"" ";

        str += @"value=" + searchMask + " ";
        str+= @"valuetype=""System.StructuredQueryType.Blurb"" localeName=""en-US""><attributes><attribute attributeID=""{9554087B-CEB6-45AB-99FF-50E8428E860D}"" clsid=""{C64B9B66-E53D-4C56-B9AE-FEDE4EE95DB1}"" chs=""0"" ";

        str += @"parsedString=""name:~ &quot;" + searchMask.TrimStart('"').TrimEnd('"') + @"&quot;"" ";
        str+= @"localeName=""en-US"" timestamp_low=""2194097220"" timestamp_high=""30601338""/></attributes></condition></attribute></attributes></condition></attribute></attributes></condition></conditions><kindList><kind name=""item""/></kindList><scope>";

        str+=@"<include path="""+ searchPath +  @""" ";
        str+=@"attributes=""1887437183""/></scope></query></persistedQuery>";
        XDocument doc = XDocument.Parse(str);

        return doc;
    }

The example code at https://github.com/nvuono/ExplorerQuickSearch uses searches by file extension only, but shows how to to create the saved search in temp folder and execute it.

An even better solution is to generate a URL for the search and supply it to Internet Explorer, which generates a Windows/File Explorer window showing the search results. Some code for doing this is shown at

Create a saved search (.search-ms) from terms in Explorer search box



来源:https://stackoverflow.com/questions/30221681/how-can-i-programmatically-use-windows-file-explorer-to-perform-a-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!