是否有 C# 方法可以从引用路径获取文件夹数

本文关键字:路径 引用 获取 文件夹 方法 是否 | 更新日期: 2023-09-27 18:35:08

我正在寻找一组简单的API方法,用于确定一个文件夹是否是另一个文件夹的子目录,以及这之间有多少步骤。像这样:

int numberOfFoldersDown(string parentFolder, string subfolder)  { ... }

似乎很有用,虽然写起来很乏味,所以我认为它应该在System.IO.PathSystem.IO.Directory程序集中的某个地方,但我在那里找不到任何有用的方法。这些函数是否可用,还是我应该自己编写?

是否有 C# 方法可以从引用路径获取文件夹数

没有内置的 AFAIK。

下面是一些同时使用 PathDirectory 方法的递归示例:

internal class Program
{
    private static void Main(string[] args)
    {
        Console.WriteLine(NumberOfFoldersDown(@"c:'temp'", @"c:'temp'"));                   // 0
        Console.WriteLine(NumberOfFoldersDown(@"c:'temp'", @"c:'temp'zz'"));                // 1
        Console.WriteLine(NumberOfFoldersDown(@"c:'temp2'", @"c:'temp'zz'"));               // -1
        Console.WriteLine(NumberOfFoldersDown(@"c:'temp2'", @"c:'temp2'zz'hui'55'"));       // 3
        Console.WriteLine(NumberOfFoldersDown(@"c:'temp2'zz'", @"c:'temp2'zz'hui'55'"));    // 2
        Console.Read();
    }
    public static int NumberOfFoldersDown(string parentFolder, string subfolder)
    {
        int depth = 0;
        WalkTree(parentFolder, subfolder, ref depth);
        return depth;
    }
    public static void WalkTree(string parentFolder, string subfolder, ref int depth)
    {
        var parent = Directory.GetParent(subfolder);
        if (parent == null)
        {
            // Root directory and no match yet
            depth = -1;
        }
        else if (0 != string.Compare(Path.GetFullPath(parentFolder).TrimEnd(''''), Path.GetFullPath(parent.FullName).TrimEnd(''''), true))
        {
            // No match yet, continue recursion
            depth++;
            WalkTree(parentFolder, parent.FullName, ref depth);
        }
    }
}

您可以遍历所有文件夹,获取路径名,一旦获得路径名,循环访问该字符串,检查每个"/",然后计算有多少个/,例如:

C:/用户/程序文件/

有 3 个/所以有 3 个跳到您想要的文件/文件夹

希望这是您想要的一些有用信息。

以某种方式找到路径,也许通过迭代它们。

当您找到一个在反斜杠上拆分路径时,请将这些部分放在一个数组中。 反转数组,遍历该数组 检查文件夹名称是否与父文件夹名称字符串匹配。 如果是这样,则返回步骤的索引 + 1?

只要

我知道唯一的方法是

parent folder : Split the path on "'" and think to remove the drive letter and colon
subfolder     : navigate it recursively (which can be time consuming)

在这方面必须考虑许多其他要点,但这是一个大主意!

编辑:好的,这是您需要的示例...还有更多的工作要做,但我认为这是一个好的开始......优点是您可以使用未知的文件夹结构,而不仅仅是表示路径的字符串。

public static class FoldersHelper
{
    public static int ParentFolderCount(string path)
    {
        int parentcnt = 0;
        if (System.IO.Directory.Exists(path))
        {
            string pathroot = Path.GetPathRoot(path);
            path = path.Remove(1, pathroot.Length);
            parentcnt = path.Split('''').Count()-1;
            return parentcnt;
        }
        else
        {
            throw new Exception("not a folder exception");
        }
        return 0;
    }
    public static int ChildFolderCount(string path)
    {
        int childcnt = 0;
        int maxchild = 0;
        if (System.IO.Directory.Exists(path))
        {
            if (Directory.GetDirectories(path).Length > 0)
            {
                foreach (string subpath in Directory.GetDirectories(path))
                {
                    childcnt = ChildFolderCount(subpath) + 1;
                    if (childcnt > maxchild) maxchild = childcnt;
                }
            }
        }
        else
        {
            throw new Exception("not a folder exception");
        }
        return maxchild;
    }
}