ComponentPro is a professional suite of libraries for FTP, sFTP and document manipulations (Word, Excel, PDF, …).

Here is a simple example of how to use it with (s)FTP.

using ComponentPro.IO; using ComponentPro.Net; using System; using System.IO; using System.Threading.Tasks; namespace FtpTest { class Program { static async Task Main(string[] args) { var hostAddress = "MY.SFTP.SERVER"; var port = 22; var userName = "MY.U$erN@mE"; var password = "MY.P@$$W0rD"; var localFileName = "test.txt"; if (!File.Exists(localFileName)) await CreateFile(localFileName); await SendToFTP(hostAddress, port, userName, password, localFileName); Console.WriteLine("\r\nPress any key..."); Console.ReadKey(); } private static async Task CreateFile(string localFileName) { using (var sw = new StreamWriter(localFileName)) { await sw.WriteLineAsync("Hello World !!!"); } } public static async Task SendToFTP(string hostAddress, int port, string userName, string password, string localFileName) { Console.WriteLine($"Connecting to (s)FTP server {hostAddress}..."); IRemoteFileSystem client = null; try { client = new Sftp(); //or Ftp() client.ReconnectionMaxRetries = 2; await client.ConnectAsync(hostAddress, port); await client.AuthenticateAsync(userName, password); Console.WriteLine(@"Connected! \o/"); await client.SetCurrentDirectoryAsync(@"/"); //Navigate to the root of the server (depending on the rights) //bool isFolderExist = await client.DirectoryExistsAsync("myFolder"); Console.WriteLine($"Sending file {localFileName} to {hostAddress}"); await client.UploadFileAsync(localFileName, localFileName); if (await client.FileExistsAsync(localFileName)) Console.WriteLine("The file has been sent successfully!"); else Console.WriteLine("Oops, the file has been lost somewhere..."); } catch (Exception ex) { Console.WriteLine($"An error occurred while sending the file:\r\n{ex.Message}\r\n"); } finally { if (client != null) client.Disconnect(); } } } }
Code language: C# (cs)