How can recursively search directories with multiple wildcards?

前端 未结 3 612
栀梦
栀梦 2021-01-22 00:12

Using C# (.NET), how can I search a file system given a directory search mask like this: (?)

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

For e

3条回答
  •  时光取名叫无心
    2021-01-22 00:56

        public static IEnumerable 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;
                }
            }
        }
    }
    

    Only the files from the first level subdirectories of each "*Image" directory are enumerated.

    Finally you can use it with:

    foreach (var path in GetImages())
                {
                    Console.WriteLine(path);
                }
    

提交回复
热议问题