C#最好的方式来获得文件夹深度为一个给定的路径? [英] C# Best way to get folder depth for a given path?
本文介绍了C#最好的方式来获得文件夹深度为一个给定的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我工作的东西,需要通过文件系统遍历和任何给定的路径,我需要知道我是如何深就是在文件夹结构。下面是我目前使用的:
I'm working on something that requires traversing through the file system and for any given path, I need to know how 'deep' I am in the folder structure. Here's what I'm currently using:
int folderDepth = 0;
string tmpPath = startPath;
while (Directory.GetParent(tmpPath) != null) {
folderDepth++;
tmpPath = Directory.GetParent(tmpPath).FullName;
}
return folderDepth;
这工作,但我怀疑有一个更好/更快的方式?非常感谢任何反馈。
This works but I suspect there's a better/faster way? Much obliged for any feedback.
推荐答案
我的头顶部:
Directory.GetFullPath().Split("\\").Length;
这篇关于C#最好的方式来获得文件夹深度为一个给定的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文