|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
What this is about?The first class, The second class, Additionally, there is a class which copies directories recursively. It's used by 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, 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.
}
To synchronize two folders you must first specify the default actions for:
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);
// 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?)To catch these events: 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
}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 When?I really don't remember how long the coding has taken, so here are the release dates (just one for now):
|
||||||||||||||||||||||