中将一个文件集合与另一个文件集合进行比较

中将一个文件集合与另一个文件集合进行比较

本文介绍了如何在C#中将一个文件集合与另一个文件集合进行比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在学习C#(现在已经花了大约2天的时间),我已经决定,出于倾斜的目的,我将重建一个用VB6制作的旧应用程序,用于同步文件(通常跨网络)

I am just learning C# (have been fiddling with it for about 2 days now) and I've decided that, for leaning purposes, I will rebuild an old app I made in VB6 for syncing files (generally across a network).

当我用VB 6编写代码时,它的工作原理大致如下:

When I wrote the code in VB 6, it worked approximately like this:

  1. 创建一个Scripting.FileSystemObject
  2. 为源和目标创建目录对象
  3. 为源和目标创建文件列表对象
  4. 遍历源对象,并检查其是否存在于目标中
    • 如果没有,请创建它
    • 如果是,请检查源版本是否较新/更大,如果是,则覆盖另一个版本
  1. Create a Scripting.FileSystemObject
  2. Create directory objects for the source and destination
  3. Create file listing objects for the source and destination
  4. Iterate through the source object, and check to see if it exists in the destination
    • if not, create it
    • if so, check to see if the source version is newer/larger, and if so, overwrite the other

到目前为止,这就是我所拥有的:

So far, this is what I have:

private bool syncFiles(string sourcePath, string destPath) {
    DirectoryInfo source = new DirectoryInfo(sourcePath);
    DirectoryInfo dest = new DirectoryInfo(destPath);

    if (!source.Exists) {
        LogLine("Source Folder Not Found!");
        return false;
    }

    if (!dest.Exists) {
        LogLine("Destination Folder Not Found!");
        return false;
    }

    FileInfo[] sourceFiles = source.GetFiles();
    FileInfo[] destFiles = dest.GetFiles();

    foreach (FileInfo file in sourceFiles) {
        // check exists on file
    }

    if (optRecursive.Checked) {
        foreach (DirectoryInfo subDir in source.GetDirectories()) {
            // create-if-not-exists destination subdirectory
            syncFiles(sourcePath + subDir.Name, destPath + subDir.Name);
        }
    }
    return true;
}

我阅读了一些似乎主张使用FileInfo或DirectoryInfo对象对"Exists"属性进行检查的示例,但我专门在寻找一种方法来搜索现有的文件集合/列表,而不是对文件进行实时检查每个文件的文件系统,因为我将通过网络进行操作并不断返回到数千个文件目录,所以它会很慢很慢.

I have read examples that seem to advocate using the FileInfo or DirectoryInfo objects to do checks with the "Exists" property, but I am specifically looking for a way to search an existing collection/list of files, and not live checks to the file system for each file, since I will be doing so across the network and constantly going back to a multi-thousand-file directory is slow slow slow.

预先感谢.

推荐答案

GetFiles()方法将仅获取确实存在的文件.它不会构成不存在的随机文件.因此,您要做的就是检查它是否在其他列表中.

The GetFiles() method will only get you files that does exist. It doesn't make up random files that doesn't exist. So all you have to do is to check if it exists in the other list.

可以执行以下操作:

var sourceFiles = source.GetFiles();
var destFiles = dest.GetFiles();

foreach (var file in sourceFiles)
{
    if(!destFiles.Any(x => x.Name == file.Name))
    {
        // Do whatever
    }
}

注意:当然,您无法保证在致电GetFiles()之后,某些事情没有改变.例如,如果您以后尝试复制文件,则该文件可能已被删除或重命名.

Note: You have of course no guarantee that something hasn't changed after you have done the calls to GetFiles(). For example, a file could have been deleted or renamed if you try to copy it later.

也许可以通过使用 Except 方法或类似方法.例如这样的东西:

Could perhaps be done nicer somehow by using the Except method or something similar. For example something like this:

var sourceFiles = source.GetFiles();
var destFiles = dest.GetFiles();

var sourceFilesMissingInDestination = sourceFiles.Except(destFiles, new FileNameComparer());

foreach (var file in sourceFilesMissingInDestination)
{
    // Do whatever
}

FileNameComparer的实现方式如下:

Where the FileNameComparer is implemented like so:

public class FileNameComparer : IEqualityComparer<FileInfo>
{
    public bool Equals(FileInfo x, FileInfo y)
    {
        return Equals(x.Name, y.Name);
    }


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

未经测试:p

这篇关于如何在C#中将一个文件集合与另一个文件集合进行比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 15:01