C# 获取从 X 开始按名称排序的目录

我需要获取另一个目录中的目录,但只有那些在 X 之后按目录名称排序的目录。


例子:


-Root

  |--DirectoryA

  |--DirectoryB

  |--DirectoryC

  |--DirectoryD

所以基本上是这样的:


foreach(string path in Directory.GetDirectories("/root/"))

{

    Console.WriteLine(path);

}

输出:


/root/DirectoryA

/root/DirectoryB

/root/DirectoryC

/root/DirectoryD

但我实际上只需要得到 C 之后的那些(包括它),我想用 Linq 应该不会太难,但我真的不知道怎么做。


也许是这样的:


Directory.GetDirectories("/root/").OrderByDescending(dirName => 

Path.GetDirectoryName(dirName)) ??


慕侠2389804
浏览 295回答 3
3回答

跃然一笑

您可以使用SkipWhile,假设顺序是按字母顺序排列的:foreach(string path in Directory.GetDirectories("/root/")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Select(dir => Path.GetFileName(dir))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .SkipWhile(dir => string.Compare(dir, "DirectoryC") < 0){&nbsp; &nbsp; Console.WriteLine(path);}这样您就可以避免在遇到第一个有效目录后进行冗余过滤。如果您只想获取文件夹,然后DirectoryC才能使用TakeWhile:foreach(string path in Directory.GetDirectories("/root/")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Select(dir => Path.GetFileName(dir))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .TakeWhile(dir => string.Compare(dir, "DirectoryC") < 0){&nbsp; &nbsp; Console.WriteLine(path);}请注意,这string.Compare取决于文化,您可以使用其他重载来自定义其行为。

翻过高山走不出你

使用Where扩展方法foreach(string path in Directory&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.GetDirectories("/root/")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.Where(d => String.Compare(Path.GetFileName(d), "DirectoryC") > 0)){&nbsp; &nbsp; Console.WriteLine(path);}如果需要,您还可以包含一个排序,但它不会影响使用Where. 如果你这样做了,Where为了效率,把它附加在后面。

繁华开满天机

或者以防万一您使用的是旧版本的 .NET,您总是可以自己编写逻辑:var directories = Directory.GetDirectories("C:\\").OrderByDescending(r => r).ToArray();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (directories != null && directories.Length > 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0, Cindex=-1; i < directories.Length; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (Cindex > 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(directories[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (directories[i] == "DirectoryC")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Cindex = i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP