【问题标题】:How to tell if a remote file path corresponds to one filter如何判断远程文件路径是否对应一个过滤器
【发布时间】:2018-11-04 06:50:25
【问题描述】:

目前,我已经在远程文件夹中获取了这些路径。

/folder 1/subfolder 2/filea.jpg

/folder 1/subfolder 2/fileb.pdf

/folder 1/subfolder 3/filea.jpg

我已经设置了过滤器来决定我是否应该根据 Windows 文件资源管理器下载路径列表中的文件,例如

/folder ?/subfolder ?/*.jpg

/*/*/abc.*

/*/*/*.*

决定是否下载的最佳方法是什么?

谢谢

【问题讨论】:

  • 你到底想做什么?您是否尝试选择目录中的所有 .jpg 文件?
  • 不,这只是一个例子,它可以是任何类型的文件。

标签: c# windows file filter explorer


【解决方案1】:

一种方法是结合使用Directory.EnumerateDirectories()Directory.GetFiles() 方法,这两种方法都接受string searchPattern 参数。

注意:要使用 Directory 类,您需要使用文件顶部的 System.IO 命名空间:

using System.IO;

我相信您必须分别枚举每个目录,因为反斜杠字符在文件或目录名称中是非法的。

例如,您可以有一个方法,该方法采用rootPath(您正在搜索的最顶层目录)并返回位于名为“subfolder?”的目录中的所有“*.jpg”文件。 (其中? 是单个字符的占位符)仅当子文件夹直接位于名为“文件夹?”的目录下时:

/// <summary>
/// Searches for files using the pattern "/folder ?/subfolder ?/*.jpg"
/// </summary>
/// <param name="rootPath">The directory in which to begin the search</param>
/// <returns>A list of file paths that meet the criteria</returns>
public static List<string> GetDownloadableFiles(string rootPath)
{
    var files = new List<string>();

    // First find all the directories that match 'folder ?', anywhere under 'rootPath'
    foreach (var directory in Directory.EnumerateDirectories(rootPath, "folder ?", 
        SearchOption.AllDirectories))
    {
        // Now find all directories directly under 'folder ?' named 'subfolder ?'
        foreach (var subDir in Directory.EnumerateDirectories(directory, "subfolder ?"))
        {
            // And add the file path for all '*.jpg' files to our list
            files.AddRange(Directory.GetFiles(subDir, "*.jpg"));
        }
    }

    return files;
}

在使用中,你会做这样的事情:

List<string> downloadableFiles = GetDownloadableFiles(@"\\server\share");

完成解决您的特定示例的方法后,我们现在可以看到如何使其更通用,以便客户端可以传入任何类型的搜索字符串。

如果我们说搜索字符串中的分隔符是正斜杠 (/),那么我们可以让用户传入字符串,然后我们可以在该字符上拆分它。这将为我们提供一个目录搜索模式数组和一个文件搜索模式(最后一项)。

一旦我们这样做了,我们就可以遍历搜索模式,获取与当前模式匹配的目录,并在每次迭代时将它们存储在一个临时列表中,直到我们到达最后一部分(即文件搜索模式),我们从哪里获得与该模式匹配的文件。

例如:

/// <summary>
/// Searches for files using the pattern defined in 'searchPattern'
/// Example search patterns: "/project ?/photoshoot ?/flowers ?/*.jpg"
///                          "/project ?/plans ?/*.pdf"
///                          "/project ?/*.txt"
/// </summary>
/// <param name="rootPath">The directory in which to begin the search</param>
/// <param name="searchPattern">The directory and file search pattern</param>
/// <returns>A list of file paths that meet the criteria</returns>
public static List<string> GetDownloadableFiles(string rootPath, string searchPattern)
{
    if (!Directory.Exists(rootPath)) 
        throw new DirectoryNotFoundException(nameof(rootPath));
    if (searchPattern == null) 
        throw new ArgumentNullException(nameof(searchPattern));

    var files = new List<string>();
    var searchParts = searchPattern.Split(new[] {'/'}, 
        StringSplitOptions.RemoveEmptyEntries);

    // This will hold the list of directories to search, and 
    // will be updated on each iteration of our loop below. 
    // We start with just one item: the 'rootPath'
    var searchFolders = new List<string> {rootPath};

    for (int i = 0; i < searchParts.Length; i++)
    {
        var subFolders = new List<string>();

        foreach (var searchFolder in searchFolders)
        {
            // If we're at the last item, it's the file pattern, so add files
            if (i == searchParts.Length - 1)
            {
                files.AddRange(Directory.GetFiles(searchFolder, searchParts[i]));
            }
            // Otherwise, add the sub directories for this pattern
            else
            {
                subFolders.AddRange(Directory.GetDirectories(searchFolder, 
                    searchParts[i]));
            }
        }

        // Reset our search folders to use the list from the latest pattern
        searchFolders = subFolders;
    }

    return files;
}

示例用法:

var files = GetDownloadableFiles(@"\\server\share", "/folder ?/subfolder ?/*.jpg");

【讨论】:

  • 问题是路径是远程路径,所以系统无法在本地检查文件。此外它可能是用户给定的任何其他类型的过滤器,所以我需要查找 .pdf 或 .pnd,这完全取决于过滤器。
  • 我展示的示例使用了远程路径。这对您的远程路径不起作用吗?此外,您可以参数化方法上的过滤器以使其更通用。我将发布一个示例。
  • 添加了一个使用根路径进行搜索的示例和一个表示搜索模式的字符串。
猜你喜欢
  • 2017-05-01
  • 1970-01-01
  • 2015-06-22
  • 1970-01-01
  • 2014-03-15
  • 1970-01-01
  • 1970-01-01
  • 2011-07-04
  • 2010-09-22
相关资源
最近更新 更多