如何比较两个文件夹的基础上在C#中的名称类似的文件? [英] How to compare two folders for similar files based on name in C#?

查看:252
本文介绍了如何比较两个文件夹的基础上在C#中的名称类似的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文件夹 A B ..内的多个文件,在那里,里面乙多个文件there..I要签入的文件与文件b中相同的文件...
我想这样一个特定的文件名

I have two folders A and B..Inside A multiple files are there and inside B multiple files are there..I have to check files in A with files in B for identical files... I tried like this for a particular file name

void DirSearch(string sDir)
        {
          List<string> lstFilesFound = new List<string>();
                foreach (string d in Directory.GetDirectories(sDir))
                    {
                        foreach (string f in Directory.GetFiles(d,"MsBuild_Tracker.proj")
                           {
                                lstFilesFound.Add(f);
                           }
                            DirSearch(d);
                    }
       }

有working..I试过像这样两个文件夹

It is working..I tried like this for two folders

foreach (string d in Directory.GetDirectories(sDir))
                {
                    foreach (string f in Directory.GetDirectories(dDir))
                    {
                        foreach (string g in Directory.GetFiles(d, f))
                        {
                            lstFilesFound.Add(g);
                        }
                        DirSearch(d, f);
                    }
                }

这是不工作...任何建议??

It is not working...Any suggestion??

推荐答案

如果你使用的 .NET 4 ,您可以使用的DirectoryInfo EnumerateFiles()。然后你可以使用LINQ加入两个目录来获得两个目录之间的共同文件。

If you're using .NET 4, you can use DirectoryInfo and EnumerateFiles(). Then you can use LINQ to join the two directories to get the common files between the two directories.

var dir1 = new DirectoryInfo(@"c:\temp1");
var dir2 = new DirectoryInfo(@"c:\temp2");

var filesinboth = from f1 in dir1.EnumerateFiles()
                  join f2 in dir2.EnumerateFiles() on f1.Name equals f2.Name
                  select f1.Name;



也可以使用,其中如果你想附加条件提出申请。

or you can use where if you want additional conditions to apply.

var filesinboth = from f1 in dir1.EnumerateFiles()
                  from f2 in dir2.EnumerateFiles()
                  where f1.Name == f2.Name // and some other condition
                  select f1.Name;



这些都将让你流的字符串秒。如果你需要的实际的FileInfo 的情况下,改变选择查询的一部分返回 F1 而不是 f1.Name

These will both give you stream of strings. If you need the actual FileInfo instances, change the select part of the query to return f1 instead of f1.Name.

如果你使用的 .NET 3.5 ,你需要使用的GetFiles()相反,它返回的FileInfo [] 。因此,查询将是这样的:

If you're using .NET 3.5, you need to use GetFiles() instead, which returns FileInfo[]. So the queries will look like this:

var filesinboth = from f1 in dir1.GetFiles()
                  join f2 in dir2.GetFiles() on f1.Name equals f2.Name
                  select f1.Name;

var filesinboth = from f1 in dir1.GetFiles()
                  from f2 in dir2.GetFiles()
                  where f1.Name == f2.Name // and some other condition
                  select f1.Name;

这篇关于如何比较两个文件夹的基础上在C#中的名称类似的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆