我正在处理需要遍历文件系统的内容,对于任何给定的路径,我需要知道我在文件夹结构中有多深。这是我目前正在使用的代码:
int folderDepth = 0;
string tmpPath = startPath;
while (Directory.GetParent(tmpPath) != null)
{
folderDepth++;
tmpPath = Directory.GetParent(tmpPath).FullName;
}
return folderDepth;
这个方法可行,但我怀疑是否有更好/更快的方法?非常感谢任何反馈。
System.IO.Path.DirectorySeparatorChar
。有点令人困惑的是,System.IO.Path.PathSeparator
是指环境变量(如%PATH%
)中路径之间的分隔符。 - mklement0