public event EventHandler<TEventArgs> Progress
Shows how to abort an operation using the Cancel method.
using System; using ComponentPro.IO; using ComponentPro.Net; ... private static void Main() { // Create a new Ftp instance. Ftp client = new Ftp(); // Connect to the FTP server. client.Connect("demo.componentpro.com"); // Authenticate. client.Authenticate("test", "test"); try { // Register an event handler. client.Progress += client_Progress; // Upload file "c:\test.zip". client.UploadFile("c:\\test.zip", "test.zip"); } catch (FtpException exc) { Console.WriteLine("Exception: " + exc.Message); } // Disconnect. client.Disconnect(); } private static void client_Progress(object sender, FileSystemProgressEventArgs e) { // Abort the uploading operation if the bytes transferred is greater than or equal to 500Kb. if (e.State == TransferState.Uploading && e.BytesTransferred >= 1024*500) { ((Ftp) sender).Cancel(); } }
Shows how to abort an operation using the Cancel method.
using System; using ComponentPro.IO; using ComponentPro.Net; ... public void DoAbort() { // Create a new Sftp instance. Sftp client = new Sftp(); // Connect to the SFTP server. client.Connect("demo.componentpro.com"); // Authenticate. client.Authenticate("test", "test"); try { // Register an event handler. client.Progress += client_Progress; // Upload file "c:\test.zip". client.UploadFile("c:\\test.zip", "test.zip"); } catch (SftpException exc) { Console.WriteLine("Exception: " + exc.Message); } // Disconnect. client.Disconnect(); } void client_Progress(object sender, FileSystemProgressEventArgs e) { // Abort the uploading operation if the bytes transferred is greater than or equal to 500Kb. if (e.State == TransferState.Uploading && e.BytesTransferred >= 1024 * 500) { ((Sftp)sender).Cancel(); } }
Shows how to use the Delete method to delete .tmp files in a directory.
using System; using ComponentPro.IO; using ComponentPro.Net; ... private static Ftp _client; static void Main() { // Create a new class instance. Ftp client = new Ftp(); _client = client; // Connect to the FTP server. client.Connect("192.168.126.128", 2222); // Authenticate. client.Authenticate("test", "test"); client.Progress += client_Progress; // ... // Delete *.tmp files client.Delete("/", false, true, "*.tmp"); // ... // Disconnect. client.Disconnect(); } static void client_Progress(object sender, ComponentPro.IO.FileSystemProgressEventArgs e) { if (e.State == TransferState.DeletingFile) { // Skip file that its name contains "my file" text. if (e.SourcePath.IndexOf("my file") != -1) e.Skip = true; } } /// <summary> /// This method is raised when user clicks on 'Cancel' button on the form. /// </summary> protected void btnCancel_Click(object sender, EventArgs e) { // Cancel the deleting operation when user clicks on the button. _client.Cancel(); }
Shows how to use DeleteDirectory method to delete .tmp files in a directory. Skip file that its name contains "my file" text.
using ComponentPro.IO; using ComponentPro.Net; ... public void DoDelete() { // Create a new class instance. Sftp client = new Sftp(); // Connect to the SFTP server. client.Connect("192.168.126.128", 2222); // Authenticate. client.Authenticate("test", "test"); client.Progress += client_Progress; // ... // Delete *.tmp files client.Delete("/", true, "*.tmp"); // ... // Disconnect. client.Disconnect(); } void client_Progress(object sender, ComponentPro.IO.FileSystemProgressEventArgs e) { if (e.State == TransferState.DeletingFile) { // Skip file that its name contains "my file" text. if (e.SourcePath.IndexOf("my file") != -1) e.Skip = true; } }
Shows how to use Download method to download files and directories on an FTP server. The download process is controlled with handlers of the TransferConfirm and Progress events.
using System; using ComponentPro.IO; using ComponentPro.Net; ... private static Ftp _client; static void Main() { // Create a new class instance. Ftp client = new Ftp(); _client = client; // Connect to the FTP server. client.Connect("demo.componentpro.com"); // Authenticate. client.Authenticate("test", "test"); // ... client.TransferConfirm += client_TransferConfirm; client.Progress += client_Progress; try { TransferOptions opt = new TransferOptions("*.*", FileOverwriteMode.Confirm); // Get all directories, subdirectories, and files in remote folder '/myfolder' to 'c:\myfolder'. client.Download("/myfolder", "c:\\myfolder", opt); } catch (Exception exc) { Console.WriteLine("Error: " + exc.Message); } // ... // Disconnect. client.Disconnect(); } /// <summary> /// This method is raised when user clicks on 'Cancel' button on the form. /// </summary> protected static void btnCancel_Click(object sender, EventArgs e) { // Cancel the download operation when user clicks on the button. _client.Cancel(); } static void client_Progress(object sender, ComponentPro.IO.FileSystemProgressEventArgs e) { // Show progress info. Console.WriteLine("Current File: %{0} completed", e.Percentage); Console.WriteLine("Total: %{0} completed", e.TotalPercentage); switch (e.State) { case TransferState.StartDownloadingFile: if (e.SourcePath.EndsWith(".exe")) { // Skip all .exe files e.Skip = true; } else if (e.SourcePath.StartsWith("/MyFolder")) { // Change the source file path if it starts with "/MyFolder" e.SourcePath = e.SourcePath.Replace("/MyFolder", "/MySecondFolder"); } break; } } static void client_TransferConfirm(object sender, ComponentPro.IO.TransferConfirmEventArgs e) { if (e.Exception != null) Console.WriteLine("Error: " + e.Exception.Message); if (e.ConfirmReason == TransferConfirmReason.FileAlreadyExists) { // Skip the existing file. e.NextAction = TransferConfirmNextActions.Skip; } Console.Write("Do you want to (r)etry or (s)kip?"); string key = Console.ReadLine(); if (key == "r") e.NextAction = TransferConfirmNextActions.Retry; else if (key == "s") e.NextAction = TransferConfirmNextActions.Skip; else // Cancel the operation. e.NextAction = TransferConfirmNextActions.Cancel; }
Shows how to use Upload methods to upload files. The system will skip existing files on the server.
using System; using ComponentPro.IO; using ComponentPro.Net; ... public void DoUpload() { // Create a new class instance. Sftp client = new Sftp(); // Connect to the SFTP server. client.Connect("demo.componentpro.com"); // Authenticate. client.Authenticate("test", "test"); // ... client.TransferConfirm += client_TransferConfirm; client.Progress += client_Progress; try { TransferOptions opt = new TransferOptions("*.*", FileOverwriteMode.Confirm); // Get all directories, subdirectories, and files in local folder 'c:\myfolder' to remote folder '/myfolder'. client.Upload("c:\\myfolder", "/myfolder", opt); } catch (Exception exc) { Console.WriteLine("Error: " + exc.Message); } // ... // Disconnect. client.Disconnect(); } void client_Progress(object sender, ComponentPro.IO.FileSystemProgressEventArgs e) { // Show progress info. Console.WriteLine("Current File: %{0} completed", e.Percentage); Console.WriteLine("Total: %{0} completed", e.TotalPercentage); switch (e.State) { case TransferState.StartUploadingFile: if (e.SourcePath.EndsWith(".exe")) { // Skip all .exe files e.Skip = true; } else if (e.SourcePath.StartsWith(@"C:\MyFolder")) { // Change the source file path if it starts with "C:\MyFolder" e.SourcePath = e.SourcePath.Replace(@"C:\MyFolder", @"C:\MySecondFolder"); } break; } } void client_TransferConfirm(object sender, ComponentPro.IO.TransferConfirmEventArgs e) { if (e.Exception != null) Console.WriteLine("Error: " + e.Exception.Message); if (e.ConfirmReason == TransferConfirmReason.FileAlreadyExists) { // Skip the existing file. e.NextAction = TransferConfirmNextActions.Skip; } Console.Write("Do you want to (r)etry or (s)kip?"); string key = Console.ReadLine(); if (key == "r") e.NextAction = TransferConfirmNextActions.Retry; else if (key == "s") e.NextAction = TransferConfirmNextActions.Skip; else // Cancel the operation. e.NextAction = TransferConfirmNextActions.Cancel; }