Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何判断远程文件路径是否对应于一个筛选器_C#_Windows_File_Filter_Explorer - Fatal编程技术网

C# 如何判断远程文件路径是否对应于一个筛选器

C# 如何判断远程文件路径是否对应于一个筛选器,c#,windows,file,filter,explorer,C#,Windows,File,Filter,Explorer,目前,我已在远程文件夹中获得这些路径 /folder 1/subfolder 2/filea.jpg /folder 1/subfolder 2/fileb.pdf /folder 1/subfolder 3/filea.jpg 我已经设置了过滤器,根据windows文件资源管理器决定是否下载路径列表中的文件,例如 /folder ?/subfolder ?/*.jpg /*/*/abc.* /*/*/*.* 决定是否下载的最佳方式是什么 多亏了,实现这一点的一种方法是使用和方法的组

目前,我已在远程文件夹中获得这些路径

/folder 1/subfolder 2/filea.jpg

/folder 1/subfolder 2/fileb.pdf

/folder 1/subfolder 3/filea.jpg
我已经设置了过滤器,根据windows文件资源管理器决定是否下载路径列表中的文件,例如

/folder ?/subfolder ?/*.jpg

/*/*/abc.*

/*/*/*.*
决定是否下载的最佳方式是什么


多亏了,

实现这一点的一种方法是使用和方法的组合,这两种方法都接受字符串searchPattern参数

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

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

例如,您可以有一个方法,该方法将根路径作为您正在搜索的最上面的目录,并返回位于名为subfolder的目录中的所有*.jpg文件?火车在哪里?仅当子文件夹直接位于名为folder的目录下时,才是单个字符的占位符吗

/// <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;
}
用法示例:

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

你到底想做什么?您正在尝试选择目录中的所有.jpg文件吗?不,这只是一个示例,它可以是任何类型的文件。问题是路径是远程路径,因此系统无法在本地检查文件。除此之外,它可以是用户提供的任何其他类型的过滤器,因此我需要查找.pdf或.pnd,这完全取决于过滤器。我展示的示例使用远程路径。这对您的远程路径不起作用吗?此外,还可以参数化方法上的过滤器,使其更通用。我将发布一个这样的示例。添加了一个以根路径进行搜索的示例和一个表示搜索模式的字符串。
/// <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");