Synchronizing folders

Glossary Item Box

Introduction

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: