C#比较基于名称的文件列表未返回完整路径

时间:2017-12-18 20:52:30

标签: c# list file compare

我正在编写一种方法,希望能够根据文件名比较来自2个不同目录的文件列表,然后将第2个目录中不存在的所有文件存储到列表中稍后使用(最有可能创建另一种复制/移动这些文件的方法)。

在查看文件列表(FilesRemaiing list)并尝试获取文件的完整路径之前,所有内容似乎都正常工作。它最终指向我的bin / debug路径。

我相信这是因为在我的查询中,我特意查询要比较的file.Name,而不是file.FullName。原因是因为FullName将返回路径以及文件名,我不能将其用于比较语句(var FilesRemaining = FileList1.Except(FileList2).ToList();)。

所以我的问题是,如何根据名称比较2组文件,然后将例外列表的完整路径存储到第3个列表中,以后我可以用它来复制/移动?

这是我的方法:

   public static List<string> CompareFiles(string ComparePath1, string ComparePath2)
    {
        List<string> FileList1 = new List<string>();
        List<string> FileList2 = new List<string>();
        List<string> CompareFileList = new List<string>();
        DirectoryInfo directory1 = new DirectoryInfo(ComparePath1);
        FileInfo[] files1 = directory1.GetFiles("*.*", SearchOption.TopDirectoryOnly);
        DirectoryInfo directory2 = new DirectoryInfo(ComparePath2);
        FileInfo[] files2 = directory2.GetFiles("*.*", SearchOption.TopDirectoryOnly);

        //Queries the given directory for filenames
        var query1 = from file in files1
                where (!file.Attributes.HasFlag(FileAttributes.Hidden))
                    select (file.Name);
        foreach (var file in query1)
        {
            FileList1.Add(file);
        }

        var query2 = from file in files2
                    where (!file.Attributes.HasFlag(FileAttributes.Hidden))
                    select (file.Name);
        foreach (var file in query2)
        {
            FileList2.Add(file);
        }

        var FilesRemaining = FileList1.Except(FileList2).ToList();

        foreach (var file in FilesRemaining)
        {
            string fullFile = Path.GetFullPath(file);
            CompareFileList.Add(fullFile);
            Console.WriteLine(fullFile);
        }

        return CompareFileList;

    }

2 个答案:

答案 0 :(得分:3)

使用此方法,您可以保留FileInfo对象,并仅使用其名称计算“例外”列表。

//More efficient way to get your files.
IEnumerable<FileInfo> files1 = directory1
    .GetFiles("*.*", SearchOption.TopDirectoryOnly)
    .Where(x => !x.Attributes.HasFlag(FileAttributes.Hidden));
IEnumerable<FileInfo> files2 = directory2
    .GetFiles("*.*", SearchOption.TopDirectoryOnly)
    .Where(x => !x.Attributes.HasFlag(FileAttributes.Hidden));

//It uses the FileNameComparer class to compare to values.
IEnumerable<FileInfo> filesRemaining = files1.Except(files2, new FileNameComparer());

,这是FileNameComparer类。

public class FileNameComparer : IEqualityComparer<FileInfo>
{
    public bool Equals(FileInfo x, FileInfo y)
    {
        //Here is where the magic is happening.
        return x.Name == y.Name;
    }

    //You need this too, altougt, I am not quite sure when it gets used.
    public int GetHashCode(FileInfo obj)
    {

        return obj.Name.GetHashCode();
    }
}

答案 1 :(得分:-1)

class Program
{
    static void Main(string[] args)
    {
        foreach ( var f in CompareFiles(@"x:\tmp\dir_1", @"x:\tmp\dir_2") )
        {
            Console.WriteLine(f);
        }

        Console.ReadKey();
    }

    public static List<string> CompareFiles(string comparePath1, string comparePath2)
    {
        //Queries the given directory for filenames
        var fileList1 = (from file in new DirectoryInfo(comparePath1).GetFiles("*.*", SearchOption.TopDirectoryOnly)
                         where !file.Attributes.HasFlag(FileAttributes.Hidden)
                         select file.Name).ToList();

        var fileList2 = (from file in new DirectoryInfo(comparePath2).GetFiles("*.*", SearchOption.TopDirectoryOnly)
                         where !file.Attributes.HasFlag(FileAttributes.Hidden)
                         select file.Name).ToList();
        return fileList1.Except(fileList2)
                        .Select(file => Path.Combine(comparePath1, file))
                        .ToList();
    }
}