First you must install the SSH.Net library from NuGet. Add the .dll as a reference to your project.
Code Snippet
- using System.IO;
- using Renci.SshNet;
- using Renci.SshNet.Common;
- using Renci.SshNet.Sftp;
- String Host = “ftp.csidata.com”;
- int Port = 22;
- String RemoteFileName = “TheDataFile.txt”;
- String LocalDestinationFilename = “TheDataFile.txt”;
- String Username = “yourusername”;
- String Password = “yourpassword”;
- using (var sftp = new SftpClient(Host, Port, Username, Password))
- {
- sftp.Connect();
- using (var file = File.OpenWrite(LocalDestinationFilename))
- {
- sftp.DownloadFile(RemoteFileName, file);
- }
- sftp.Disconnect();
- }