3
我正在编写检索某些文件匹配模式的C#函数。使用通配符解析规范路径
输入:C:\ abc * \ abc?\ testfile。*
输出:所有文件匹配。
我以为我可以通过递归。但是,这是不容易:(
你有一个很好的算法
更新:
我做到了感谢基伦:)
void PrintAllFiles(DirectoryInfo currentDir,
string currentPattern, string nextPatten)
{
DirectoryInfo[] dis = currentDir.GetDirectories(currentPattern);
if (dis.Length > 0)
{
string[] remainPattern = nextPatten.Split("\\".ToCharArray());
if (remainPattern.Length > 0)
{
foreach (DirectoryInfo di in dis)
{
PrintAllFiles(di, remainPattern.First(),
string.Join("\\", remainPattern.Skip(1).ToArray()));
}
}
}
FileInfo[] fis = currentDir.GetFiles(currentPattern);
foreach (FileInfo fi in fis)
{
Console.WriteLine(fi.DirectoryName + "\\" + fi.Name);
}
}
而不是'“\\”'你应该使用'Path.DirectorySeparatorChar' – Oliver 2010-07-22 12:23:44
同意。但是在盒子里代表太长了。 – Benjamin 2010-07-22 13:35:21