public sealed class QuickSyncOptions : ICloneable
The following example demonstrates how to handle the Progress event to report the current progress state in a Mirror operation and skip a file.
using System; using ComponentPro.IO; using ComponentPro.Net; ... static void Main() { Ftp client = new Ftp(); // Connect to the FTP server. client.Connect("192.168.126.128", 21); // Authenticate. client.Authenticate("test", "test"); // Register event handler. client.Progress += client_Progress; // Create a new instance of the MirrorOptions class. QuickSyncOptions opt = new QuickSyncOptions(); // Set synchronization's settings // Synchronize files with different modification date time or different file size. // Files with the same size and modification date time will not be synchronized. opt.Comparer = FileComparers.FileLastWriteTimeComparer & FileComparers.FileSizeComparer; opt.Recursive = true; // Synchronize folders. client.QuickSynchronize( "", // Source directory path. "c:\\test", // Destination directory path. true, // Source directory is master. It means "c:\test" will be identical to the remote dir. opt ); // Synchronize files with different attributes and different file size. // Files with the same attributes or size will not be synchronized. opt.Comparer = FileComparers.FileAttributesComparer | FileComparers.FileSizeComparer; opt.Recursive = true; // Synchronize folders. client.QuickSynchronize( "my remote dir", // Source directory path. "c:\\test", // Destination directory path. false, // Destination directory is master. It means remote directory "my remote dir" will be identical to the local dir "c:\test". opt ); // Synchronize files that have different contents. // Files with the same content will not be synchronized. opt.Comparer = FileComparers.FileContentComparer; opt.Recursive = true; // Synchronize folders. client.QuickSynchronize( "my remote dir", // Source directory path. "c:\\test", // Destination directory path. false, // Destination directory is master. It means remote directory "my remote dir" will be identical to the local dir "c:\test". opt ); // Do something here // ... client.Disconnect(); } static void client_Progress(object sender, FileSystemProgressEventArgs e) { switch (e.State) { // For files case TransferState.DeletingFile: case TransferState.StartUploadingFile: case TransferState.StartDownloadingFile: Console.WriteLine(string.Format("Sync - Applying change on file '{0}', change type: {1}", e.SourceFileInfo.Name, e.State)); if (e.SourceFileInfo.Name == "my file") e.Skip = true; // we skip the changes. break; case TransferState.FileDeleted: case TransferState.FileUploaded: case TransferState.FileDownloaded: Console.WriteLine(string.Format("Sync - change applied on file '{0}', change type: {1}", e.SourceFileInfo.Name, e.State)); break; // For directory case TransferState.DeletingDirectory: case TransferState.CopyingDirectory: case TransferState.CreatingDirectory: Console.WriteLine(string.Format("Sync - Applying change on directory '{0}', change type: {1}", e.SourceFileInfo.Name, e.State)); if (e.SourceFileInfo.Name == "my folder") e.Skip = true; // we skip the changes. break; case TransferState.DirectoryDeleted: case TransferState.DirectoryCreated: Console.WriteLine(string.Format("Sync - change applied on directory '{0}', change type: {1}", e.SourceFileInfo.Name, e.State)); if (e.SourceFileInfo.Name == "my folder") e.Skip = true; // we skip the changes. break; } }
Shows how to use file comparer classes and synchronize files and folders.
using System; using ComponentPro.IO; using ComponentPro.Net; ... void SynchronizeFilesDirs() { Sftp client = new Sftp(); // Connect to the SFTP server. client.Connect("192.168.126.128", 22); // Authenticate. client.Authenticate("test", "test"); // Register event handler. client.Progress += client_Progress; // Create a new instance of the QuickSyncOptions class. QuickSyncOptions opt = new QuickSyncOptions(); // Set synchronization's settings // Synchronize files with different modification date time or different file size. // Files with the same size and modification date time will not be synchronized. opt.Comparer = FileComparers.FileLastWriteTimeComparer & FileComparers.FileSizeComparer; opt.Recursive = true; // Synchronize folders. client.QuickSynchronize( "", // Source directory path. "c:\\test", // Destination directory path. true, // Source directory is master. It means "c:\test" will be identical to the remote dir. opt ); // Synchronize files with different attributes and different file size. // Files with the same attributes or size will not be synchronized. opt.Comparer = FileComparers.FileAttributesComparer | FileComparers.FileSizeComparer; opt.Recursive = true; // Synchronize folders. client.QuickSynchronize( "my remote dir", // Source directory path. "c:\\test", // Destination directory path. false, // Destination directory is master. It means remote directory "my remote dir" will be identical to the local dir "c:\test". opt ); // Synchronize files that have different contents. // Files with the same content will not be synchronized. opt.Comparer = FileComparers.FileContentComparer; opt.Recursive = true; // Synchronize folders. client.QuickSynchronize( "my remote dir", // Source directory path. "c:\\test", // Destination directory path. false, // Destination directory is master. It means remote directory "my remote dir" will be identical to the local dir "c:\test". opt ); // Do something here // ... client.Disconnect(); } void client_Progress(object sender, FileSystemProgressEventArgs e) { switch (e.State) { // For files case TransferState.DeletingFile: case TransferState.StartUploadingFile: case TransferState.StartDownloadingFile: Console.WriteLine(string.Format("Sync - Applying change on file '{0}', change type: {1}", e.SourceFileInfo.Name, e.State)); if (e.SourceFileInfo.Name == "my file") e.Skip = true; // we skip the changes. break; case TransferState.FileDeleted: case TransferState.FileUploaded: case TransferState.FileDownloaded: Console.WriteLine(string.Format("Sync - change applied on file '{0}', change type: {1}", e.SourceFileInfo.Name, e.State)); break; // For directory case TransferState.DeletingDirectory: case TransferState.CopyingDirectory: case TransferState.CreatingDirectory: Console.WriteLine(string.Format("Sync - Applying change on directory '{0}', change type: {1}", e.SourceFileInfo.Name, e.State)); if (e.SourceFileInfo.Name == "my folder") e.Skip = true; // we skip the changes. break; case TransferState.DirectoryDeleted: case TransferState.DirectoryCreated: Console.WriteLine(string.Format("Sync - change applied on directory '{0}', change type: {1}", e.SourceFileInfo.Name, e.State)); if (e.SourceFileInfo.Name == "my folder") e.Skip = true; // we skip the changes. break; } }
Shows how to use file comparer classes and synchronize files and folders.
using ComponentPro.IO; using ComponentPro.Compression; ... void SynchronizeFilesDirs() { Zip zip = new Zip(); // Open an existing archive. zip.Open("test.zip", System.IO.FileMode.Open); zip.Progress += zip_Progress; // Create a new instance of the QuickSyncOptions class. QuickSyncOptions opt = new QuickSyncOptions(); // Set synchronization's settings // Synchronize files with different modification date time or different file size. // Files with the same size and modification date time will not be synchronized. opt.Comparer = FileComparers.FileLastWriteTimeComparer & FileComparers.FileSizeComparer; opt.Recursive = true; // Synchronize folders. zip.QuickSynchronize( "", // Source directory path within the archive. "" indicates the current directory within the archive. "c:\\test", // Destination directory path. true, // Source directory is master. It means "c:\test" will be identical to the directory within the archive. opt ); // Synchronize files with different attributes and different file size. // Files with the same attributes or size will not be synchronized. opt.Comparer = FileComparers.FileAttributesComparer | FileComparers.FileSizeComparer; opt.Recursive = true; // Synchronize folders. zip.QuickSynchronize( "/my dir", // Source directory path. "c:\\test", // Destination directory path. false, // Destination directory is master. It means directory within the archive named "/my dir" will be identical to the local dir "c:\test". opt ); // Synchronize files that have different contents. // Files with the same content will not be synchronized. opt.Comparer = FileComparers.FileContentComparer; opt.Recursive = true; // Synchronize folders. zip.QuickSynchronize( "my remote dir", // Source directory path. "c:\\test", // Destination directory path. false, // Destination directory is master. It means remote directory "my remote dir" will be identical to the local dir "c:\test". opt ); // Do something here // ... zip.Close(); } private void zip_Progress(object sender, FileSystemProgressEventArgs e) { // Skip deleting file with .doc extension. If you do not want to delete any files, always set e.Skip to true. if (e.State == TransferState.DeletingFile && e.SourcePath.EndsWith(".doc")) { // Skip. e.Skip = true; } }