C# Windows 窗体,检查两个文件夹之间的差异,如果发现差异,则将一个文件夹克隆到另一个文件夹中

C# Windows Form, checking for differences between two folders, and cloning one folder into the other if differences are found

我正在使用 C# 开发 Windows 表单应用程序。

我有两个不同的文件夹位置,其中一个由 Google 文件流(Google Drive 软件)更新。

我需要将此 Google 文件 Stream 文件夹克隆到我本地 D 盘上的其他文件夹中,并在我的 Windows 表单打开时更新它。

我正在考虑比较这两个文件夹之间的差异,然后将差异从一个文件夹复制到下一个文件夹。

我不知道该怎么做,如有任何帮助,我们将不胜感激。

如果您的源文件夹是普通 windows 文件夹(由 google 文件流更新,而不是 google 文件流文件夹),您应该能够使用 'FileSystemWatcher' class。此 class 监视文件夹中的指定更改并在它们发生时引发事件(您的应用程序随后可以处理)。

这是我在需要做类似的事情时使用的一篇文章(观察文件夹的变化)How to work with file system watcher in C#

以防文章下线,这里是直接从中提取的摘要:

The following code snippet shows how the MonitorDirectory method would look like. This method would be used to monitor a particular directory and raise events whenever a change occurs. The directory path is passed as an argument to the method.

private static void MonitorDirectory(string path)
        {
            FileSystemWatcher fileSystemWatcher = new FileSystemWatcher();
            fileSystemWatcher.Path = path;
            fileSystemWatcher.Created += FileSystemWatcher_Created;
            fileSystemWatcher.Renamed += FileSystemWatcher_Renamed;
            fileSystemWatcher.Deleted += FileSystemWatcher_Deleted;
            fileSystemWatcher.EnableRaisingEvents = true;
        }

您需要替换为您自己的事件处理程序(或使用上述名称实现)。

为了获得额外奖励,您还可以使用 FileSystemWatcher 的过滤器 属性 来定位特定文件类型。

有关 Microsoft Docs

的完整文档