C# 通过交叉检查在两个文件夹中查找新文件

C# 通过交叉检查在两个文件夹中查找新文件,c#,filter,filtering,.net,C#,Filter,Filtering,.net,我正在尝试将两个文件夹排序到一个修补过的文件夹中,查找新文件夹中的新文件并将其标记为新文件,以便只能传输该文件。我不在乎日期或散列更改。新文件夹中不在旧文件夹中的文件 不知怎的,这条线 pf.NFile = !( oldPatch.FindAll(s => s.Equals(f)).Count() == 0); 总是返回错误。我的交叉核对逻辑有问题吗 List<string> newPatch = DirectorySearch(_newFolder); List<st

我正在尝试将两个文件夹排序到一个修补过的文件夹中,查找新文件夹中的新文件并将其标记为新文件,以便只能传输该文件。我不在乎日期或散列更改。新文件夹中不在旧文件夹中的文件

不知怎的,这条线

pf.NFile = !( oldPatch.FindAll(s => s.Equals(f)).Count() == 0);
总是返回错误。我的交叉核对逻辑有问题吗

List<string> newPatch = DirectorySearch(_newFolder);
List<string> oldPatch = DirectorySearch(_oldFolder);

foreach (string f in newPatch)
{
    string filename = Path.GetFileName(f);
    string Dir = (Path.GetDirectoryName(f).Replace(_newFolder, "") + @"\");
    PatchFile pf = new PatchFile();
    pf.Dir = Dir;
    pf.FName = filename;
    pf.NFile = !( oldPatch.FindAll(s => s.Equals(f)).Count() == 0);
    nPatch.Files.Add(pf);
 }

foreach (string f in oldPatch)
{
    string filename = Path.GetFileName(f);
    string Dir = (Path.GetDirectoryName(f).Replace(_oldFolder, "") + @"\");
    PatchFile pf = new PatchFile();
    pf.Dir = Dir;
    pf.FName = filename;

    if (!nPatch.Files.Exists(item => item.Dir == pf.Dir && 
                             item.FName == pf.FName))
    {
        nPatch.removeFiles.Add(pf);
    }
}
List newPatch=DirectorySearch(\u newFolder);
List oldPatch=DirectorySearch(_oldFolder);
foreach(newPatch中的字符串f)
{
字符串文件名=Path.GetFileName(f);
string Dir=(Path.GetDirectoryName(f).Replace(\u newFolder,“”+@“\”);
PatchFile pf=新的PatchFile();
pf.Dir=Dir;
pf.FName=文件名;
pf.NFile=!(oldPatch.FindAll(s=>s.Equals(f)).Count()==0);
nPatch.Files.Add(pf);
}
foreach(oldPatch中的字符串f)
{
字符串文件名=Path.GetFileName(f);
string Dir=(Path.GetDirectoryName(f).Replace(\u oldFolder,“”+@“\”);
PatchFile pf=新的PatchFile();
pf.Dir=Dir;
pf.FName=文件名;
如果(!nPatch.Files.Exists)(item=>item.Dir==pf.Dir&&
item.FName==pf.FName))
{
nPatch.removeFiles.Add(pf);
}
}

我没有您正在使用的类(如
DirectorySearch
PatchFile
),因此我无法编译您的代码,但我的行
\u oldPatch.FindAll(…
不会返回任何内容,因为您正在比较完整路径(
c:\oldPatch\filea.txt
不是
c:\newpatch\filea.txt
)而且不仅仅是文件名。在我看来,您的算法可以简化,类似于以下伪代码(使用
List.Contains
而不是
List.FindAll
):


我在
A
中有
1.xml,2.xml,3.xml
,在
B
中有
1.xml,3.xml
。输出是
2.xml
-在
B
中缺少
B

你能检查你是否只比较名称或完整路径与文件名吗?你只需要比较名称-路径总是不同的。
var _newFolder = "d:\\temp\\xml\\b";
var _oldFolder = "d:\\temp\\xml\\a";
List<FileInfo> missing = new List<FileInfo>();
List<FileInfo> nPatch = new List<FileInfo>();
List<FileInfo> newPatch = new DirectoryInfo(_newFolder).GetFiles().ToList();
List<FileInfo> oldPatch = new DirectoryInfo(_oldFolder).GetFiles().ToList();
// take all files in new patch
foreach (var f in newPatch)
{
    nPatch.Add(f);
}
// search for hits in old patch
foreach (var f in oldPatch)
{
    if (!nPatch.Select (p => p.Name.ToLower()).Contains(f.Name.ToLower()))
    {
        missing.Add(f);
    }
}
// new files are in missing
var locationA = "d:\\temp\\xml\\a";
var locationB = "d:\\temp\\xml\\b";
// takes file names from A and B and put them into lists
var filesInA = new DirectoryInfo(locationA).GetFiles().Select (n => n.Name).ToList();
var filesInB = new DirectoryInfo(locationB).GetFiles().Select (n => n.Name).ToList();
// Except retrieves all files that are in A but not in B
foreach (var file in filesInA.Except(filesInB).ToList())
{
    Console.WriteLine(file);
}