C# 获取给定路径的文件夹深度的最佳方法?

发布于 2024-07-10 08:50:04 字数 326 浏览 1 评论 0原文

我正在做一些需要遍历文件系统的事情,对于任何给定的路径,我需要知道我在文件夹结构中的“深度”。 这是我目前正在使用的:

int folderDepth = 0;
string tmpPath = startPath;

while (Directory.GetParent(tmpPath) != null) 
{
    folderDepth++;
    tmpPath = Directory.GetParent(tmpPath).FullName;
}
return folderDepth;

这可行,但我怀疑有更好/更快的方法吗? 非常感谢任何反馈。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

莫多说 2024-07-17 08:50:04

从我的头顶上掉下来:

Directory.GetFullPath().Split("\\").Length;

Off the top of my head:

Directory.GetFullPath().Split("\\").Length;
清风挽心 2024-07-17 08:50:04

我对此已经很晚了,但我想指出保罗·索尼尔的答案可能是最短的,但应该是:

 Path.GetFullPath(tmpPath).Split(Path.DirectorySeparatorChar).Length;

I'm more than late on this but I wanted to point out Paul Sonier's answer is probably the shortest but should be:

 Path.GetFullPath(tmpPath).Split(Path.DirectorySeparatorChar).Length;
樱花落人离去 2024-07-17 08:50:04

我一直很喜欢递归解决方案。 效率低下,但很有趣!

public static int FolderDepth(string path)
{
    if (string.IsNullOrEmpty(path))
        return 0;
    DirectoryInfo parent = Directory.GetParent(path);
    if (parent == null)
        return 1;
    return FolderDepth(parent.FullName) + 1;
}

我喜欢用 C# 编写的 Lisp 代码!

这是我更喜欢的另一个递归版本,并且可能更有效:

public static int FolderDepth(string path)
{
    if (string.IsNullOrEmpty(path))
        return 0;
    return FolderDepth(new DirectoryInfo(path));
}

public static int FolderDepth(DirectoryInfo directory)
{
    if (directory == null)
        return 0;
    return FolderDepth(directory.Parent) + 1;
}

好时光,好时光......

I'm always a fan the recursive solutions. Inefficient, but fun!

public static int FolderDepth(string path)
{
    if (string.IsNullOrEmpty(path))
        return 0;
    DirectoryInfo parent = Directory.GetParent(path);
    if (parent == null)
        return 1;
    return FolderDepth(parent.FullName) + 1;
}

I love the Lisp code written in C#!

Here's another recursive version that I like even better, and is probably more efficient:

public static int FolderDepth(string path)
{
    if (string.IsNullOrEmpty(path))
        return 0;
    return FolderDepth(new DirectoryInfo(path));
}

public static int FolderDepth(DirectoryInfo directory)
{
    if (directory == null)
        return 0;
    return FolderDepth(directory.Parent) + 1;
}

Good times, good times...

摘星┃星的人 2024-07-17 08:50:04

如果您使用 Path 类的成员,则可以处理路径分隔字符的本地化和其他与路径相关的注意事项。 以下代码提供深度(包括根)。 它对坏字符串等的鲁棒性并不强,但它对你来说是一个开始。

int depth = 0;
do
{
    path = Path.GetDirectoryName(path);
    Console.WriteLine(path);
    ++depth;
} while (!string.IsNullOrEmpty(path));

Console.WriteLine("Depth = " + depth.ToString());

If you use the members of the Path class, you can cope with localizations of the path separation character and other path-related caveats. The following code provides the depth (including the root). It's not robust to bad strings and such, but it's a start for you.

int depth = 0;
do
{
    path = Path.GetDirectoryName(path);
    Console.WriteLine(path);
    ++depth;
} while (!string.IsNullOrEmpty(path));

Console.WriteLine("Depth = " + depth.ToString());
め可乐爱微笑 2024-07-17 08:50:04

假设您的路径已经过验证,在 .NET 3.5 中您还可以使用 LINQ 在 1 行代码中完成此操作...

Console.WriteLine(@"C:\Folder1\Folder2\Folder3\Folder4\MyFile.txt".Where(c
=> c = @"\").Count);

Assuming your path has already been vetted for being valid, in .NET 3.5 you could also use LINQ to do it in 1 line of code...

Console.WriteLine(@"C:\Folder1\Folder2\Folder3\Folder4\MyFile.txt".Where(c
=> c = @"\").Count);

仙女 2024-07-17 08:50:04

如果目录末尾有反斜杠,您会得到与没有反斜杠时不同的答案。 这是该问题的可靠解决方案。

string pathString = "C:\\temp\\"
var rootFolderDepth = pathString.Split(Path.DirectorySeparatorChar).Where(i => i.Length > 0).Count();

这将返回路径长度 2。如果在不使用 where 语句的情况下执行此操作,则会得到路径长度 3,如果省略最后一个分隔符,则会得到路径长度 2。

If the directory has a backslash at the end, you get a different answer than when it doesn't. Here's a robust solution to the problem.

string pathString = "C:\\temp\\"
var rootFolderDepth = pathString.Split(Path.DirectorySeparatorChar).Where(i => i.Length > 0).Count();

This returns a path length of 2. If you do it without the where statement, you get a path length of 3 or a path length of 2 if you omit the last separator character.

懒的傷心 2024-07-17 08:50:04

也许有人还需要一些性能测试......

        double linqCountTime = 0;
        double stringSplitTime = 0;
        double stringSplitRemEmptyTime = 0;
        int linqCountFind = 0;
        int stringSplitFind = 0;
        int stringSplitRemEmptyFind = 0;

        string pth = @"D:\dir 1\complicated dir 2\more complicated dir 3\much more complicated dir 4\only dir\another complicated dir\dummy\dummy.dummy.45682\";

        //Heat Up
        DateTime dt = DateTime.Now;
        for (int i = 0; i < 10000; i++)
        {
            linqCountFind = pth.Count(c => c == '\\');
        }
         _= DateTime.Now.Subtract(dt).TotalMilliseconds;
        dt = DateTime.Now;
        for (int i = 0; i < 10000; i++)
        {
            stringSplitFind = pth.Split('\\').Length;
        }
        _ = DateTime.Now.Subtract(dt).TotalMilliseconds;
        dt = DateTime.Now;
        for (int i = 0; i < 10000; i++)
        {
            stringSplitRemEmptyFind = pth.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries).Length;
        }
        _ = DateTime.Now.Subtract(dt).TotalMilliseconds;
        dt = DateTime.Now;

        //Testing
        dt = DateTime.Now;
        for (int i = 0; i < 1000000; i++)
        {
            linqCountFind = pth.Count(c => c == '\\');
        }
        linqCountTime = DateTime.Now.Subtract(dt).TotalMilliseconds; //linq.Count: 1390 ms

        dt = DateTime.Now;
        for (int i = 0; i < 1000000; i++)
        {
            stringSplitFind = pth.Split('\\').Length-1;
        }
        stringSplitTime = DateTime.Now.Subtract(dt).TotalMilliseconds; //string.Split: 715 ms

        dt = DateTime.Now;
        for (int i = 0; i < 1000000; i++)
        {
            stringSplitRemEmptyFind = pth.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries).Length;
        }
        stringSplitRemEmptyTime = DateTime.Now.Subtract(dt).TotalMilliseconds; // string.Split with RemoveEmptyEntries option: 720 ms

        string linqCount = "linqCount - Find: "+ linqCountFind + "; Time: "+ linqCountTime.ToString("F0") +" ms"+ Environment.NewLine;
        string stringSplit = "stringSplit - Find: " + stringSplitFind + "; Time: " + stringSplitTime.ToString("F0") + " ms" + Environment.NewLine;
        string stringSplitRemEmpty = "stringSplitRemEmpty - Find: " + stringSplitRemEmptyFind + "; Time: " + stringSplitRemEmptyTime.ToString("F0") + " ms" + Environment.NewLine;

        MessageBox.Show(linqCount + stringSplit + stringSplitRemEmpty);

        // Results:
        // linqCount - Find: 9;  Time: 1390 ms
        // stringSplit - Find: 9;  Time: 715 ms
        // stringSplitRemEmpty - Find: 9;  Time: 720 ms

Maybe someone need also some performance testing...

        double linqCountTime = 0;
        double stringSplitTime = 0;
        double stringSplitRemEmptyTime = 0;
        int linqCountFind = 0;
        int stringSplitFind = 0;
        int stringSplitRemEmptyFind = 0;

        string pth = @"D:\dir 1\complicated dir 2\more complicated dir 3\much more complicated dir 4\only dir\another complicated dir\dummy\dummy.dummy.45682\";

        //Heat Up
        DateTime dt = DateTime.Now;
        for (int i = 0; i < 10000; i++)
        {
            linqCountFind = pth.Count(c => c == '\\');
        }
         _= DateTime.Now.Subtract(dt).TotalMilliseconds;
        dt = DateTime.Now;
        for (int i = 0; i < 10000; i++)
        {
            stringSplitFind = pth.Split('\\').Length;
        }
        _ = DateTime.Now.Subtract(dt).TotalMilliseconds;
        dt = DateTime.Now;
        for (int i = 0; i < 10000; i++)
        {
            stringSplitRemEmptyFind = pth.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries).Length;
        }
        _ = DateTime.Now.Subtract(dt).TotalMilliseconds;
        dt = DateTime.Now;

        //Testing
        dt = DateTime.Now;
        for (int i = 0; i < 1000000; i++)
        {
            linqCountFind = pth.Count(c => c == '\\');
        }
        linqCountTime = DateTime.Now.Subtract(dt).TotalMilliseconds; //linq.Count: 1390 ms

        dt = DateTime.Now;
        for (int i = 0; i < 1000000; i++)
        {
            stringSplitFind = pth.Split('\\').Length-1;
        }
        stringSplitTime = DateTime.Now.Subtract(dt).TotalMilliseconds; //string.Split: 715 ms

        dt = DateTime.Now;
        for (int i = 0; i < 1000000; i++)
        {
            stringSplitRemEmptyFind = pth.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries).Length;
        }
        stringSplitRemEmptyTime = DateTime.Now.Subtract(dt).TotalMilliseconds; // string.Split with RemoveEmptyEntries option: 720 ms

        string linqCount = "linqCount - Find: "+ linqCountFind + "; Time: "+ linqCountTime.ToString("F0") +" ms"+ Environment.NewLine;
        string stringSplit = "stringSplit - Find: " + stringSplitFind + "; Time: " + stringSplitTime.ToString("F0") + " ms" + Environment.NewLine;
        string stringSplitRemEmpty = "stringSplitRemEmpty - Find: " + stringSplitRemEmptyFind + "; Time: " + stringSplitRemEmptyTime.ToString("F0") + " ms" + Environment.NewLine;

        MessageBox.Show(linqCount + stringSplit + stringSplitRemEmpty);

        // Results:
        // linqCount - Find: 9;  Time: 1390 ms
        // stringSplit - Find: 9;  Time: 715 ms
        // stringSplitRemEmpty - Find: 9;  Time: 720 ms
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文