如何以编程方式使用Windows文件资源管理器执行搜索?

时间:2023-01-17 08:26:10

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.

我们的网络中有一个文件夹,我们想在其中搜索,包括我们程序中的子文件夹。我们想要返回一个名称包含“String1”或“String2”或“StringN”的文件列表。我们希望以编程方式打开资源管理器窗口,并使用本机Windows文件资源管理器查看与搜索结果匹配的所有文件。

Is this possible? How?

这可能吗?怎么样?

Thanks!

3 个解决方案

#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.

您可以使用.ms-search文件格式表示已保存的搜索。如果您打开此文件格式,它将启动应用了搜索条件的文件资源管理器。

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.

由于Windows资源管理器随着操作系统的每个版本而变化...我建议通过.NET进行搜索并在表单上的网格上显示结果。

Use the System.IO namespace.

使用System.IO命名空间。

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

System.IO.Directory.GetFiles(folderName)将获取文件...

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:

下面是一些代码,用于创建已保存的搜索xml文件(file.search-ms),以便按名称搜索指定的路径和搜索字符串:

/// <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.

https://github.com/nvuono/ExplorerQuickSearch上的示例代码仅使用文件扩展名搜索,但显示如何在temp文件夹中创建已保存的搜索并执行它。

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

更好的解决方案是为搜索生成URL并将其提供给Internet Explorer,从而生成显示搜索结果的Windows /文件资源管理器窗口。执行此操作的一些代码显示在

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

从资源管理器搜索框中的术语创建已保存的搜索(.search-ms)

#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.

您可以使用.ms-search文件格式表示已保存的搜索。如果您打开此文件格式,它将启动应用了搜索条件的文件资源管理器。

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.

由于Windows资源管理器随着操作系统的每个版本而变化...我建议通过.NET进行搜索并在表单上的网格上显示结果。

Use the System.IO namespace.

使用System.IO命名空间。

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

System.IO.Directory.GetFiles(folderName)将获取文件...

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:

下面是一些代码,用于创建已保存的搜索xml文件(file.search-ms),以便按名称搜索指定的路径和搜索字符串:

/// <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.

https://github.com/nvuono/ExplorerQuickSearch上的示例代码仅使用文件扩展名搜索,但显示如何在temp文件夹中创建已保存的搜索并执行它。

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

更好的解决方案是为搜索生成URL并将其提供给Internet Explorer,从而生成显示搜索结果的Windows /文件资源管理器窗口。执行此操作的一些代码显示在

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

从资源管理器搜索框中的术语创建已保存的搜索(.search-ms)