2

使用 C# (.NET),如何在给定目录搜索掩码的情况下搜索文件系统:(?)

\\server\Scanner\images\*Images\*\*_*

例如,我需要先找到所有顶级目录:

\\server\Scanner\images\Job1Images
\\server\Scanner\images\Job2Images

...然后我需要继续使用搜索掩码:

\\server\Scanner\images\Job1Images\*\*_*
\\server\Scanner\images\Job2Images\*\*_*

这似乎并不太复杂,但我无法为我的生活弄明白......

如上所述,我使用的是 C# 和 .NET。搜索可能试图定位目录或文件。(即 *.txt 或 <*Directory>)

4

4 回答 4

2

像这样:

顶级目录:

  //get Top level
   string[] TopLevel = Directory.GetDirectories(path);

然后您将不得不使用通配符模式对此文件夹执行递归函数,例如:

 // Only get subdirectories that begin with the letter "p." 
            string pattern = "p*";
            string[] dirs = folder.GetDirectories(path, pattern);

我建议您使用通配符来获取数组输出,如果使用递归函数或直接查询路径,您将找出最佳方法。

于 2012-11-20T17:01:19.403 回答
1

编辑:啊,.NET 4 的新功能,因此您不必执行递归函数(感谢 Matthew Brubaker)

IEnumerable<String> matchingFilePaths2 = System.IO.Directory.EnumerateFiles(@"C:\some folder to start in", filePatternToMatchOn, System.IO.SearchOption.AllDirectories);




第一个答案:

//get all files that have an underscore - searches all folders under the start folder
List<String> matchingFilePaths = new List<string>();
String filePatternToMatchOn = "*_*";
FileUtilities.GetAllFilesMatchingPattern(@"C:\some folder to start in", ref matchingFilePaths, filePatternToMatchOn);

...

public static void GetAllFilesMatchingPattern(String pathToGetFilesIn, ref List<String> fullFilePaths, String searchPattern)
{
    //get all files in current directory that match the pattern
    String[] filePathsInCurrentDir = Directory.GetFiles(pathToGetFilesIn, searchPattern);
    foreach (String fullPath in filePathsInCurrentDir)
    {
        fullFilePaths.Add(fullPath);
    }

    //call this method recursively for all directories
    String[] directories = Directory.GetDirectories(pathToGetFilesIn);
    foreach (String path in directories)
    {
        GetAllFilesMatchingPattern(path, ref fullFilePaths, searchPattern);
    }
}
于 2012-11-20T17:49:04.587 回答
1
    public static IEnumerable<string> GetImages()
{    
    //For each "*Image" directory
    foreach (var jobFolder in Directory.EnumerateDirectories(@"\\server\Scanner\images", "*Images"))
    {
        //For each first level subdirectory
        foreach (var jobSubFolder in Directory.EnumerateDirectories(jobFolder))
        {
            //Enumerate each file containing a '_'
            foreach (var filePath in Directory.EnumerateFiles(jobSubFolder, "*_*", SearchOption.TopDirectoryOnly))
            {                               
                yield return filePath;
            }
        }
    }
}

仅枚举每个“*Image”目录的第一级子目录中的文件。

最后你可以使用它:

foreach (var path in GetImages())
            {
                Console.WriteLine(path);
            }
于 2012-11-20T18:58:00.877 回答
0

有一个 C# 过程,您可以使用 * 和 ? 等通配符按路径模式搜索文件夹。

示例如果将路径模式 C:\Folder?*\Folder2 传递给过程,则将返回文件夹路径列表

C:\文件夹1\A\文件夹2

C:\文件夹A\B\文件夹2

...

等等

static List<string> GetFoldersByPathPattern(string folderPathPattern)
{
    List<string> directories = new List<string>();
    directories.Add("");

    string[] folderParts = folderPathPattern.Split(new char[] { '\\' }, StringSplitOptions.None);
    foreach (string folderPart in folderParts)
    {

        if (folderPart.Contains('*') || folderPart.Contains('?'))
        {
            List<string> newDirectories = new List<string>();

            foreach (string directory in directories)
            {
                foreach (string newDirectory in Directory.GetDirectories(directory, folderPart))
                {
                    newDirectories.Add(newDirectory);
                }
            }

            directories = newDirectories;
        }
        else
        {
            for (int i = 0; i < directories.Count(); i++)
            {
                directories[i] = directories[i] + folderPart + "\\";
            }
        }
    }

    return directories;
}
于 2021-08-25T12:30:51.130 回答