This topic demonstrates how to synchronize disk files (and disk files and disk folders).
Basic steps
To synchronizes files, the following steps must be performed:
-
Retrieve references to the files you wish to synchronize, using AbstractFile or AbstractFile-derived classes. Retrieve references to folders if you will also be synchronizing folders, using AbstractFolder or AbstractFolder-derived classes.
-
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 files. Note that when synchronizing only files, files are synchronized to the most recent file regardless of the name. Therefore, the code below will result in file1.txt, file2.txt, and file3.txt being synchronized to the most recently modified file, even though the file names differ.
VB.NET |
Copy Code |
Imports Xceed.FileSystem Imports Xceed.Synchronize
Try Dim file1 As AbstractFile = New DiskFile("D:\temp\file1.txt") Dim file2 As AbstractFile = New DiskFile("D:\temp\file2.txt") Dim file3 As AbstractFile = New DiskFile("D:\temp\file3.txt") Dim syncOptions As New SynchronizationOptions()
syncOptions.UseMetaData = False 'By default, this value is true.
Try Synchronizer.EasySynchronize(file1, file2, file3, 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 { AbstractFile file1 = new DiskFile(@"D:\temp\file1.txt"); AbstractFile file2 = new DiskFile(@"D:\temp\file2.txt"); AbstractFile file3 = new DiskFile(@"D:\temp\file3.txt");
SynchronizationOptions syncOptions = new SynchronizationOptions(); syncOptions.UseMetaData = false; //By default, this value is true.
try { Synchronizer.EasySynchronize(file1, file2, file3, syncOptions); } catch (Exception eSynch) { Console.WriteLine("Synch error: {0}", eSynch.ToString()); } } catch (Exception ePrepSynch) { Console.WriteLine("Prep-synch error: {0}", ePrepSynch.ToString()); }
|
The following example demonstrates how to synchronize disk files, specifying which file will be used as the master file using the MasterItemParameter class.
VB.NET |
Copy Code |
Imports Xceed.FileSystem Imports Xceed.Synchronize
Try Dim file1 As AbstractFile = New DiskFile("D:\temp\file1.txt") Dim file2 As AbstractFile = New DiskFile("D:\temp\file2.txt") Dim file3 As AbstractFile = New DiskFile("D:\temp\file3.txt") Dim masterFile As MasterItemParameter = New MasterItemParameter (file2 ) Dim syncOptions As New SynchronizationOptions()
syncOptions.UseMetaData = False 'By default, this value is true.
Try Synchronizer.EasySynchronize(file1, file2, file3, masterFile, 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 { AbstractFile file1 = new DiskFile(@"D:\temp\file1.txt"); AbstractFile file2 = new DiskFile(@"D:\temp\file2.txt"); AbstractFile file3 = new DiskFile(@"D:\temp\file3.txt");
MasterItemParameter masterFile = new MasterItemParameter(file2); SynchronizationOptions syncOptions = new SynchronizationOptions(); syncOptions.UseMetaData = false; //By default, this value is true.
try { Synchronizer.EasySynchronize(file1, file2, file3, masterFile, syncOptions); } catch (Exception eSynch) { Console.WriteLine("Synch error: {0}", eSynch.ToString()); } } catch (Exception ePrepSynch) { Console.WriteLine("Prep-synch error: {0}", ePrepSynch.ToString()); }
|
The following example demonstrates how to synchronize files and folders together. Note that when synchronizing files and folders, synchronization only occurs between the files passed to the synchronization method and the corresponding files in the passed folders. As a result, the code below results in synchronization only occurring between D:\temp\file1.txt and any copies of file1.txt that exist in folder01 and folder02. Any other files in the folders will not be synchronized.
VB.NET |
Copy Code |
Imports Xceed.FileSystem Imports Xceed.Synchronize
Try Dim file1 As AbstractFile = New DiskFile("D:\temp\file1.txt") Dim folder1 As AbstractFolder = New DiskFolder("D:\temp\folder01") Dim folder2 As AbstractFolder = New DiskFolder("D:\temp\folder02") Dim syncOptions As New SynchronizationOptions()
syncOptions.UseMetaData = False 'By default, this value is true.
Try Synchronizer.EasySynchronize(file1, folder1, folder2, 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 { AbstractFile file1 = new DiskFile(@"D:\temp\file1.txt"); AbstractFolder folder1 = new DiskFolder(@"D:\temp\folder01"); AbstractFolder folder2 = new DiskFolder(@"D:\temp\folder02");
SynchronizationOptions syncOptions = new SynchronizationOptions(); syncOptions.UseMetaData = false; //By default, this value is true.
try { Synchronizer.EasySynchronize(file1, folder1, folder2); } 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 manually which file will be used as the master? Use a MasterItemParameter created from one of the files being passed for synchronization (see above for an example). 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 file specified as the master is older than the target files.
Do you want to specify that a given target file should be deleted? Use a MasterItemParameter created using a using an AbstractFile-derived file object whose underlying physical file does not exist.
Do you want force a file to be created in a given location when synchronizing files only? Pass an AbstractFile-derived object whose underlying physical file does not exist. When synchronizing files and folders together, any file passed to the synchronization method will automatically be created in any folder that does not contain that file.