What this is about?
The first class, FolderDiff
, gets differences between files in a folder and in all subfolders and raises an event for each file. It looks for different file sizes and missing files or folders.
The second class, FolderSync
, uses the events raised by the first one and reacts as defined by a default behavior.
Additionally, there is a class which copies directories recursively. It's used by FolderSync
to copy missing folders.
Why?
There are maybe many reasons to have a program to synchronize two folders. It's possible to use it as a very simple backup program, to avoid needing to copy all data on every backup. A little like a incremental backup. You may also find it useful if you have a portable MP3 or OGG (Yes! Ogg Vorbis! It's free !!!! OpenSource rules !!!) player. You can create a mirror of the player on your disk and then synchronize instead of always making the same changes in both places.
How?
The three classes, FolderDiff
, FolderSync
, RecursiveCopy
, are in the FolderSynchronisation
namespace. To get the differences between two folders:
void GetDifferencies()
{
FolderDiff fDiff = new FolderDiff(@"C:\folder1", @"C:\folder2");
fDiff.CompareEvent += new FolderSynchronisation.CompareDelegate(Compared);
fDiff.Compare();
}
private void Compared(ComparisonResult result,
System.IO.FileSystemInfo[] sysInfo, bool isAFolder)
{
}
To synchronize two folders you must first specify the default actions for:
- Files with different sizes
- Files or folders missing in the first folder
- Files or folders missing in the second folder
Like this for example:
FileActions defSize = FileActions.OverwriteNewer;
FileActions defMissing1 = FileActions.Ask;
FileActions defMissing2 = FileActions.Ignore;
Then you have to create the
FolderSync
:
FolderSync fSync = new FolderSync(@"C:\Folder1", @"C:\Folder2",
defMissing2, defMissing1, defSize);
Then you need to catch the events:
fSync.AskWhatToDo +=
new FolderSynchronisation.AskWhatToDoDelegate(
AskUserEvent_Handler);
fSync.ErrorEvent += new FolderSynchronisation.ErrorDelegate(
ErrorEvent_Handler);
To catch these events:
private void ErrorEvent_Handler(Exception e, string[] fileNames)
{
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)
{
AskActionForm askForm = new AskActionForm(files, isADir, missingIndex);
askForm.ShowDialog(this);
return askForm.DialogResult;
}
Finally, the
Sync()
method must be called to start the synchronisation.
fSync.Sync();
What else?
The first version of this program (which will never be published, because that's some kind of things I prefer you not to know about...) never really worked and had a horrible structure. Its FolderDiff
class only scanned one dir which rendered it unable to find missing folders in this dir. When I saw what was necessary to correct those errors, I decided to begin from scratch, which became this code.
When?
I really don't remember how long the coding has taken, so here are the release dates (just one for now):
- June 13th 2004 - First version