我正在做一些需要遍历文件系统和任何给定路径的事情,我需要知道我在文件夹结构中的“深度”。这是我目前正在使用的:
int folderDepth = 0;
string tmpPath = startPath;
while (Directory.GetParent(tmpPath) != null)
{
folderDepth++;
tmpPath = Directory.GetParent(tmpPath).FullName;
}
return folderDepth;
这行得通,但我怀疑还有更好/更快的方法吗?非常感谢任何反馈。
最佳答案
在我的脑海中:
Directory.GetFullPath().Split("\\").Length;
关于C# 获取给定路径的文件夹深度的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/316009/