适用于 .NET 的 FolderSync 类






3.80/5 (9投票s)
2004年6月15日
2分钟阅读

118735

4356
一组类,
这是关于什么的?
第一个类,FolderDiff
,获取文件夹及其所有子文件夹中文件的差异,并为每个文件引发事件。它查找不同的文件大小以及丢失的文件或文件夹。
第二个类,FolderSync
,使用第一个类引发的事件,并根据默认行为做出反应。
此外,还有一个类可以递归地复制目录。它由 FolderSync
用于复制丢失的文件夹。
为什么?
可能有许多原因需要一个程序来同步两个文件夹。它可以作为非常简单的备份程序使用,以避免每次备份时都需要复制所有数据。有点像增量备份。如果您有一个便携式 MP3 或 OGG(是的!Ogg Vorbis!它是免费的!!!!开源规则!!!)播放器,您也可能会觉得它有用。您可以在磁盘上创建播放器的镜像,然后同步,而不是始终在两个地方进行相同的更改。
如何做到?
这三个类,FolderDiff
、FolderSync
、RecursiveCopy
,位于 FolderSynchronisation
命名空间中。要获取两个文件夹之间的差异
void GetDifferencies()
{
FolderDiff fDiff = new FolderDiff(@"C:\folder1", @"C:\folder2");
fDiff.CompareEvent += new FolderSynchronisation.CompareDelegate(Compared);
// The event raised for each file
fDiff.Compare(); // Starts comparing
}
private void Compared(ComparisonResult result,
System.IO.FileSystemInfo[] sysInfo, bool isAFolder)
{
// result is the result of the comparison
// sysInfo are the two FileSystemInfo for the concerned files or folders
// As you can see, I do not use FileInfo objects because
// it wouldn't work with folders.
// isAFolder is true when sysInfo are folders
// With this, you can know if it's possible to convert
// sysInfo to either System.IO.DirectoryInfo
// or to System.IO.FileInfo.
// The differencies may then be listed in listboxes, as in the example app.
}
要同步两个文件夹,您首先必须指定以下内容的默认操作:
- 文件大小不同
- 第一个文件夹中丢失的文件或文件夹
- 第二个文件夹中丢失的文件或文件夹
FileActions defSize = FileActions.OverwriteNewer;
FileActions defMissing1 = FileActions.Ask;
FileActions defMissing2 = FileActions.Ignore;
然后您必须创建 FolderSync
FolderSync fSync = new FolderSync(@"C:\Folder1", @"C:\Folder2",
defMissing2, defMissing1, defSize);
然后您需要捕获事件fSync.AskWhatToDo +=
new FolderSynchronisation.AskWhatToDoDelegate(
AskUserEvent_Handler);
// This event is raised when the defaultAction is
// set to FileActions.Ask and
// the user must be asked what to do
fSync.ErrorEvent += new FolderSynchronisation.ErrorDelegate(
ErrorEvent_Handler);
// This is raised when an error somewhere occurs (
// Which is never the case, since errors
// do not exist, isn't it?)
要捕获这些事件private void ErrorEvent_Handler(Exception e, string[] fileNames)
{
// e contains the thrown exception, and fileNames are
// the two files which were compared,
// copied or whatever
MessageBox.Show("Exception " + e.Message +
" was thrown.\nAdditional Data:\n"
+ fileNames[0] + "\n" + fileNames[1],"Exception",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
private FileActions AskUserEvent_Handler(
System.IO.FileSystemInfo[] files, bool isADir,
int missingIndex)
{
// This is my implementation in the example app to
// let the user choose the action.
// It creates and displays another form which shows
// the two files and possbile choices
// missingIndex is either -1 if the files have different sizes,
// or 1 or 2 to specify in
// which folder the files are missing.
AskActionForm askForm = new AskActionForm(files, isADir, missingIndex);
askForm.ShowDialog(this); // this shows the askForm
// on owner this, the active form
return askForm.DialogResult; // returns the user choice
// to the caller, which is fSync
}
最后,必须调用 Sync()
方法才能开始同步。fSync.Sync();
还有什么?
这个程序的第一个版本(永远不会发布,因为那是某种我不希望你知道的事情......)实际上并没有真正工作,并且结构很糟糕。它的 FolderDiff
类只扫描了一个目录,这使得它无法找到该目录中丢失的文件夹。当我看到纠正这些错误需要什么时,我决定从头开始,这最终变成了这段代码。
什么时候?
我真的不记得编码花了多长时间,所以这里是发布日期(目前只有一个):
- 2004 年 6 月 13日 - 第一个版本