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;