比较两个文件夹 - 模板与实际
我正在尝试将模板文件夹与子文件夹与现有目录结构进行比较。我试图递归地遍历每个目录中的所有子文件夹。我的代码如下。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您收到错误,因为
recursiveProjectdir
的范围仅在foreach 循环
内You are getting error because scope of
recursiveProjectdir
is only insideforeach loop
将最后一个比较目录移到 foreach 循环内。只是修改了您的测试方法。
Move the last compareDirectories inside foreach loop. Just modified your method for testing.