如何在 C# 中将一组文件与另一组文件进行比较?

发布于 2024-08-14 06:37:21 字数 1328 浏览 8 评论 0 原文

我刚刚学习 C#(已经摆弄它大约 2 天了),我决定,出于学习目的,我将重建一个用 VB6 制作的旧应用程序,用于同步文件(通常通过网络)。

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

  1. 创建一个 Scripting.FileSystemObject
  2. 为源和目标创建目录对象
  3. 为源和目标创建文件列表对象
  4. 迭代源对象,并检查目的地是否存在
    • 如果没有,请创建它
    • 如果是,请检查源版本是否更新/更大,如果是,则覆盖另一个

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

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 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).

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

  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;
}

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.

Thanks in Advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

靖瑶 2024-08-21 06:37:21

GetFiles() 方法只会获取确实存在的文件。它不会随机生成不存在的文件。所以你所要做的就是检查它是否存在于另一个列表中。

以下内容可能有效:

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

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

注意:您当然不能保证在调用 后某些内容没有发生变化GetFiles()。例如,如果您稍后尝试复制文件,则该文件可能已被删除或重命名。


通过使用Except 方法或类似的方法。例如这样的事情:

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

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

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

FileNameComparer 的实现方式如下:

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

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.

Something in the lines of this could work:

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

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

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.


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
}

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();
    }
}     

Untested though :p

遇见了你 2024-08-21 06:37:21

一个小细节,

 sourcePath + subDir.Name

我会使用

 System.IO.Path.Combine(sourcePath, subDir.Name)

Path 对文件名和文件夹名进行可靠的、独立于操作系统的操作。

我还注意到 optRecursive.Checked 不知从何而来。作为良好设计的一个问题,请将其作为一个参数:

bool syncFiles(string sourcePath, string destPath, bool checkRecursive)

既然您提到它可能用于大量文件,请留意 .NET 4,它有一个 IEnumerable 替代 GetFiles() ,可以让您处理这个以流媒体的方式。

One little detail, instead of

 sourcePath + subDir.Name

I would use

 System.IO.Path.Combine(sourcePath, subDir.Name)

Path does reliable, OS independent operations on file- and foldernames.

Also I notice optRecursive.Checked popping out of nowhere. As a matter of good design, make that a parameter:

bool syncFiles(string sourcePath, string destPath, bool checkRecursive)

And since you mention it may be used for large numbers of files, keep an eye out for .NET 4, it has an IEnumerable replacement for GetFiles() that will let you process this in a streaming fashion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文