[英]Is there a way to fetch all directories and files matching a pattern with wildcards recursively in C#?
I am looking for a library that allows me to take something similar to this:我正在寻找一个允许我使用类似于以下内容的库:
./*
./*/*.*
./**
./**/*.*
./*.cs
./folder1/*.png
Then i would need to pass it to a method which scans the filesystem and returns these paths:然后我需要将它传递给一个扫描文件系统并返回这些路径的方法:
C:\folder1, C:\folder2, C:\folder3
C:\folder1\file.cs,C:\folder1\test.dll,[...], C:\folder2\image.png, [...]
C:\folder1, C:\folder1\results, C:\folder2, [...]
C:\file1.cs, C:\file2.cs, C:\file3.cs
C:\folder1\image1.png, C:\folder1\image2.png, [...]
I am aware that Directory.GetFiles()
and Directory.GetDirectories
exist and that they accept a filter, but i need one method that does this recursively and flexibly and returns a set of absolute paths.我知道
Directory.GetFiles()
和Directory.GetDirectories
存在并且它们接受过滤器,但是我需要一种方法来递归和灵活地执行此操作并返回一组绝对路径。
One option is to use a library like Glob library which unlike .net file globbing does folders with wild cards.一种选择是使用像Glob 库这样的库,它不像 .net 文件通配符使用通配符来处理文件夹。
var folder = @"Your path goes here";
string[] folderPatterns = { "**/bin", "**/obj" };
string[] filePatterns = { "**/*cs"};
var results = new List<string>();
foreach (var pattern in folderPatterns)
{
results.AddRange(Glob.Directories(folder, pattern).ToArray());
}
foreach (var pattern in filePatterns)
{
results.AddRange(Glob.Files(folder, pattern).ToArray());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.