This topic demonstrates how to synchronize disk folders.
Basic steps
To synchronizes folders, the following steps must be performed:
-
Retrieve references to the folders you wish to synchronize, using AbstractFolder-derived classes. With Xceed's FileSystem-based products, a folder is a folder; it does not matter if it is located on an FTP server, on disk or in memory.
-
Optionally, retrieve a reference to a SynchronizationOptions and set its properties as desired.
- Call the static Synchronizer.EasySynchronize method.
Demonstration
The following example demonstrates how to synchronize disk folders. Note that when synchronizing only folders, files within the folders are synchronized to the most recent version of the correspondingly named file.
VB.NET |
Copy Code |
Imports Xceed.FileSystem Imports Xceed.Synchronize
Try Dim folder1 As AbstractFolder = New DiskFolder("D:\temp\folder1") Dim folder2 As AbstractFolder = New DiskFolder("D:\temp\folder2") Dim folder3 As AbstractFolder = New DiskFolder("D:\temp\folder3") Dim syncOptions As New SynchronizationOptions()
syncOptions.UseMetaData = False 'By default, this value is true.
Try Synchronizer.EasySynchronize(folder1, folder2, folder3, syncOptions) Catch eSynch As Exception Console.WriteLine("Synch error: {0}", eSynch.ToString()) End Try Catch ePrepSynch As Exception Console.WriteLine("Prep-synch error: {0}", ePrepSynch.ToString()) End Try
|
C# |
Copy Code |
using Xceed.FileSystem; using Xceed.Synchronize;
try { AbstractFolder folder1 = new DiskFolder(@"D:\temp\folder1"); AbstractFolder folder2 = new DiskFolder(@"D:\temp\folder2"); AbstractFolder folder3 = new DiskFolder(@"D:\temp\folder3");
SynchronizationOptions syncOptions = new SynchronizationOptions(); syncOptions.UseMetaData = false; //By default, this value is true.
try { Synchronizer.EasySynchronize(folder1, folder2, folder3); } catch (Exception eSynch) { Console.WriteLine("Synch error: {0}", eSynch.ToString()); } } catch (Exception ePrepSynch) { Console.WriteLine("Prep-synch error: {0}", ePrepSynch.ToString()); }
|
Things you should consider
Here are the main questions you should ask yourself when synchronizing files:
-
Do you want to filter the items that will be synchronized? Use
filters.
-
Do you want to specify which folder will be used as the master? Create a
MasterItemParameter using one of the folders being passed for synchronization.
-
Do you want to specify manually which folder will be used as the master? Use a
MasterItemParameter created from one of the folders being passed for synchronization. Note that in certain situations, a conflict can arise (see
File synchronization for more details) and a
Conflict event will be triggered, most notably if the files in the folder specified as the master are older than the target files.
-
Do you want to specify that a given target folder should be deleted? Use a MasterItemParameter created using a using an
AbstractFolder-derived folder object whose underlying physical item does not exist.
-
Do you want force a folder to be created in a given location? Pass an
AbstractFolder-derived object whose underlying physical item does not exist.