我需要列出路径中的所有目录。我只需要获取最后一个子目录路径,而不是所有路径。
即。对
pathbase / 2016/01
我不需要pathbase / 2016而是pathbase / 2016/01
现在我使用此代码:
List<string> dirs = new List<string>(Directory.EnumerateDirectories(pathBase));
string[] entries = System.IO.Directory.GetDirectories(pathBase, "*",
SearchOption.AllDirectories);
var listDir = from dir
in entries
return listDir.ToList();
我可以在Linq中使用哪种类型的过滤器来排除此目录?
感谢。
答案 0 :(得分:5)
你的意思是你需要所有leafs目录(没有子目录的目录)?
如果是这样,应该这样做
// First off, don't use GetDirectories on the static Directory class, it returns a string path and not a DirectoryInfo which isn't very usefull here, instead create a DirectoryInfo and go from there:
var RootDirectory = new DirectoryInfo(pathBase);
// Then queries all of it's subdirectories and filter on those
var listDir = RootDirectory.GetDirectories("*",SearchOption.AllDirectories)
.Where(dir=>!dir.GetDirectories().Any())
.ToList();