对比非完全相同的文件,两个文件夹? [英] comparing two folders for non identical files?

查看:112
本文介绍了对比非完全相同的文件,两个文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

     var dir1Files = dir1.GetFiles("*", SearchOption.AllDirectories).Select(x => new { x.Name, x.Length });
    var dir2Files = dir2.GetFiles("*", SearchOption.AllDirectories).Select(x => new { x.Name, x.Length });
    var onlyIn1 = dir1Files.Except(dir2Files).Select(x => new { x.Name });
    File.WriteAllLines(@"d:\log1.txt", foo1);



我在这里基于名称,然后在文本文件中写入... $ B $比较两个文件b,而我需要用目录名一起写名字......但我不能选择这样的目录名

Here i am comparing two files based on name and writing in a text file... But i need to write name along with directory name...But i cannot select directory name like this

 var onlyIn1 = dir1Files.Except(dir2Files).Select(x => new { x.Name,x.DirectoryName });



任何建议?

Any suggestion?

推荐答案

是否使用全名解决问题了吗?

Does using FullName solve your problem?

var onlyIn1 = dir1Files.Except(dir2Files).Select(x => new { x.FullName }); 



另外,您可以放在一起选择中的自定义字符串语句:

// get strings in the format "file.ext, c:\path\to\file"
var onlyIn1 = dir1Files.Except(dir2Files).Select(x => 
                  string.Format("{0}, {1}", x.Name, x.Directory.FullName)); 



为了能够使用中的对象信息,不要创建匿名类型有限在第一个步骤的信息,而是保持满的FileInfo 对象:

var dir1Files = dir1.GetFiles("*", SearchOption.AllDirectories);
var dir2Files = dir2.GetFiles("*", SearchOption.AllDirectories);



更新结果
在你的代码的真正问题示例是除了将比较使用的默认的相等比较的。对于大多数类型的(当然在匿名类型的情况下),这意味着它将比较对象的引用。既然你有的FileInfo 对象两个列表,除了将返回所有的物体从第一个列表,其中相同的对象实例的未在第二找到。因为没有在第一个列表对象实例都存在同样在第二,第一个列表中的所有对象将被退回。

Update
The real problem in your code sample is that Except will compare using the default equality comparer. For most types (and certainly in the case of anonymous types) this means that it will compare the object references. Since you have two lists with FileInfo objects, Except will return all objects from the first list, where the same object instances are not found in the second. Since none of the object instances in the first list are present also in the second, all objects from the first list will be returned.

为了解决这一问题(现在依然有机会获得你想要存储的数据),您将需要使用 除了重载接受一个的IEqualityComparer< T> 。首先,让我们创建的IEqualityComparer

In order to fix this (and still have access to the data that you want to store) you will need to use the Except overload that accepts an IEqualityComparer<T>. First, let's create the IEqualityComparer:

class FileInfoComparer : IEqualityComparer<FileInfo>
{
    public bool Equals(FileInfo x, FileInfo y)
    {
        // if x and y are the same instance, or both are null, they are equal
        if (object.ReferenceEquals(x,y))
        {
            return true;
        }
        // if one is null, they are not equal
        if (x==null || y == null)
        {
            return false;
        }
        // compare Length and Name
        return x.Length == y.Length && 
            x.Name.Equals(y.Name, StringComparison.OrdinalIgnoreCase);
    }

    public int GetHashCode(FileInfo obj)
    {
        return obj.Name.GetHashCode() ^ obj.Length.GetHashCode();
    }
}

现在您可以使用比较器对文件进行比较目录:

Now you can use that comparer to compare the files in the directories:

var dir1 = new DirectoryInfo(@"c:\temp\a");
var dir2 = new DirectoryInfo(@"c:\temp\b");

var dir1Files = dir1.GetFiles("*", SearchOption.AllDirectories); 
var dir2Files = dir2.GetFiles("*", SearchOption.AllDirectories); 

var onlyIn1 = dir1Files
    .Except(dir2Files, new FileInfoComparer())
    .Select(x =>  string.Format("{0}, {1}", x.Name, x.Directory.FullName));

这篇关于对比非完全相同的文件,两个文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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