简体   繁体   English

如何以编程方式使用Windows File Explorer执行搜索?

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

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". 我们要返回名称包含“ String1”或“ String2”或“ 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. 我们希望以编程方式打开“浏览器”窗口,并使用本机Windows文件浏览器查看与搜索结果匹配的所有文件。

Is this possible? 这可能吗? How? 怎么样?

Thanks! 谢谢!

You can use the .ms-search file format to express a saved search. 您可以使用.ms-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. 如果您已经打开了文件浏览器窗口并应用了搜索,则可以使用功能区上的“保存搜索”按钮将其保存。

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. 您将需要一个递归函数来首先枚举所有文件,然后递归每个子目录。

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. 更好的解决方案是生成用于搜索的URL并将其提供给Internet Explorer, Internet Explorer会生成一个显示搜索结果的Windows / File Explorer窗口。 Some code for doing this is shown at 一些执行此操作的代码显示在

Create a saved search (.search-ms) from terms in Explorer search box 根据资源管理器搜索框中的字词创建保存的搜索(.search-ms)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 打开 Windows 资源管理器并执行搜索 - Open Windows Explorer and perform a search 如何使用语音识别执行网络搜索 - How can I use speech recognition to perform a web search 如何获取“文件资源管理器”打开的窗口的目录? - How can i get the directories of File Explorer opened windows? (如何)使用Windows文件资源管理器查找应用程序? - (How to) Use Windows file explorer to find an application? 如何以编程方式编辑Windows 7 / Server 2008中的hosts文件? - How can I programmatically edit the hosts file in Windows 7/Server 2008? 如何在 Windows 资源管理器中打开文件夹? - How can I open a folder in Windows Explorer? 在Windows资源管理器中单击文件时,如何覆盖发生的情况? - How can I override what happens when I click a file in Windows Explorer? 如何在Visual Studio中以编程方式执行“转到定义”? - How can I perform “Go To Definition” programmatically in Visual Studio? 没有Process类的情况下,如何在Windows 8文件浏览器中打开应用程序文件夹? - How can I open the application folder in the Windows 8 file explorer without the Process class? 如何在项目浏览器中的“读取,写入”中访问文本文件,该文件不在Windows Phone 7的独立存储中? - How can I access a text file in the project explorer “read, write” which is not in the isolated storage in Windows Phone 7?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM