简体   繁体   English

在c#中组织文件路径

[英]organize file paths in c#

is there a way if organizing paths depending on which one is more towards the root. 是否有办法组织路径取决于哪一个更靠近根。 For example if I have the paths: 例如,如果我有路径:

"C:\someFolder\program files\b"
"C:\someFolder\X"
"C:\Z"
"C:\someFolder\program files\a"

then I will like to sort them as: 然后我想把它们分类为:

"C:\Z"
"C:\someFolder\X"
"C:\someFolder\program files\a"
"C:\someFolder\program files\b"

I am actually trying to create a tree view and that's why I want to sort them like that. 我实际上是在尝试创建一个树视图,这就是为什么我要像那样对它们进行排序。

怎么样:

files.OrderBy(x => x.Split('\\').Length).ThenBy(x => x)

You could do a 你可以做一个

pathName.Split('/').Length pathName.Split( '/')。长度

on the path to get the number of levels deep, then sort on that number. 在获取深度级别的路径上,然后对该数字进行排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM