比较两个文件夹 - 模板与实际

发布于 2024-12-26 05:59:21 字数 1035 浏览 0 评论 0原文

我正在尝试将模板文件夹与子文件夹与现有目录结构进行比较。我试图递归地遍历每个目录中的所有子文件夹。我的代码如下。

public void compareDirectories(DirectoryInfo Templatedir, DirectoryInfo Projectdir)
{
    DirectoryInfo[] recursiveTemplatedirs = Templatedir.GetDirectories("*.*");
    DirectoryInfo[] recursiveProjectdirs = Projectdir.GetDirectories("*.*");

    string recursiveName;
    string projectName;

    foreach (DirectoryInfo recursiveTemplatedir in recursiveTemplatedirs) 
    {
        recursiveName = recursiveTemplatedir.Name.ToString();
        foreach (DirectoryInfo recursiveProjectdir in recursiveProjectdirs)
        {
            projectName = recursiveProjectdir.Name.ToString();
            if (recursiveName == projectName)
            {
                lstTest.Items.Add("Match " + recursiveName);
            }
            else lstTest.Items.Add("No Match " + recursiveName);
        }

        compareDirectories(recursiveTemplatedir, recursiveProjectdir);
    }
}

当我尝试运行此命令时,出现错误:recursiveProjectdir 在此上下文中不存在。关于如何让它运行有什么想法吗?

I am trying to compare a template folder with subfolders with an existing directory structure. I am trying to recursively iterate through all the sub folders in each directory. My code is as follows.

public void compareDirectories(DirectoryInfo Templatedir, DirectoryInfo Projectdir)
{
    DirectoryInfo[] recursiveTemplatedirs = Templatedir.GetDirectories("*.*");
    DirectoryInfo[] recursiveProjectdirs = Projectdir.GetDirectories("*.*");

    string recursiveName;
    string projectName;

    foreach (DirectoryInfo recursiveTemplatedir in recursiveTemplatedirs) 
    {
        recursiveName = recursiveTemplatedir.Name.ToString();
        foreach (DirectoryInfo recursiveProjectdir in recursiveProjectdirs)
        {
            projectName = recursiveProjectdir.Name.ToString();
            if (recursiveName == projectName)
            {
                lstTest.Items.Add("Match " + recursiveName);
            }
            else lstTest.Items.Add("No Match " + recursiveName);
        }

        compareDirectories(recursiveTemplatedir, recursiveProjectdir);
    }
}

When I try to run this I get the error that recursiveProjectdir does not exist in this context. Any thoughts on how to get this to run?

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

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

发布评论

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

评论(2

一曲爱恨情仇 2025-01-02 05:59:21

您收到错误,因为 recursiveProjectdir 的范围仅在 foreach 循环

foreach (DirectoryInfo recursiveTemplatedir in recursiveTemplatedirs) 
    {
        recursiveName = recursiveTemplatedir.Name.ToString();
        foreach (DirectoryInfo recursiveProjectdir in recursiveProjectdirs)
        {
        }
        //recursiveProjectdir doesn't exists outside the foreach loop
        compareDirectories(recursiveTemplatedir, recursiveProjectdir);
    }

You are getting error because scope of recursiveProjectdir is only inside foreach loop

foreach (DirectoryInfo recursiveTemplatedir in recursiveTemplatedirs) 
    {
        recursiveName = recursiveTemplatedir.Name.ToString();
        foreach (DirectoryInfo recursiveProjectdir in recursiveProjectdirs)
        {
        }
        //recursiveProjectdir doesn't exists outside the foreach loop
        compareDirectories(recursiveTemplatedir, recursiveProjectdir);
    }
ぶ宁プ宁ぶ 2025-01-02 05:59:21

将最后一个比较目录移到 foreach 循环内。只是修改了您的测试方法。

public static void compareDirectories(DirectoryInfo Templatedir, DirectoryInfo Projectdir)
{
    DirectoryInfo[] recursiveTemplatedirs = Templatedir.GetDirectories("*.*");
    DirectoryInfo[] recursiveProjectdirs = Projectdir.GetDirectories("*.*");

    string recursiveName;
    string projectName;

    foreach (DirectoryInfo recursiveTemplatedir in recursiveTemplatedirs) 
    {
        recursiveName = recursiveTemplatedir.Name.ToString();
        foreach (DirectoryInfo recursiveProjectdir in recursiveProjectdirs)
        {
            projectName = recursiveProjectdir.Name.ToString();
            if (recursiveName == projectName) {
              Console.WriteLine("Match " + recursiveName);
            } else 
              Console.WriteLine("No Match " + recursiveName);

        compareDirectories(recursiveTemplatedir, recursiveProjectdir);
        }

    }
}

Move the last compareDirectories inside foreach loop. Just modified your method for testing.

public static void compareDirectories(DirectoryInfo Templatedir, DirectoryInfo Projectdir)
{
    DirectoryInfo[] recursiveTemplatedirs = Templatedir.GetDirectories("*.*");
    DirectoryInfo[] recursiveProjectdirs = Projectdir.GetDirectories("*.*");

    string recursiveName;
    string projectName;

    foreach (DirectoryInfo recursiveTemplatedir in recursiveTemplatedirs) 
    {
        recursiveName = recursiveTemplatedir.Name.ToString();
        foreach (DirectoryInfo recursiveProjectdir in recursiveProjectdirs)
        {
            projectName = recursiveProjectdir.Name.ToString();
            if (recursiveName == projectName) {
              Console.WriteLine("Match " + recursiveName);
            } else 
              Console.WriteLine("No Match " + recursiveName);

        compareDirectories(recursiveTemplatedir, recursiveProjectdir);
        }

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文