FTP
Use the below code to download a file from an FTP server with C#.
Code Snippet
- using System.Net;
- using System.IO;
- String RemoteFtpPath = “ftp://ftp.csidata.com:21/Futures.20150305.gz”;
- String LocalDestinationPath = “Futures.20150305.gz”;
- String Username=“yourusername”;
- String Password = “yourpassword”;
- Boolean UseBinary = true; // use true for .zip file or false for a text file
- Boolean UsePassive = false;
- FtpWebRequest request = (FtpWebRequest)WebRequest.Create(RemoteFtpPath);
- request.Method = WebRequestMethods.Ftp.DownloadFile;
- request.KeepAlive = true;
- request.UsePassive = UsePassive;
- request.UseBinary = UseBinary;
- request.Credentials = new NetworkCredential(Username, Password);
- FtpWebResponse response = (FtpWebResponse)request.GetResponse();
- Stream responseStream = response.GetResponseStream();
- StreamReader reader = new StreamReader(responseStream);
- using (FileStream writer = new FileStream(LocalDestinationPath, FileMode.Create))
- {
- long length = response.ContentLength;
- int bufferSize = 2048;
- int readCount;
- byte[] buffer = new byte[2048];
- readCount = responseStream.Read(buffer, 0, bufferSize);
- while (readCount > 0)
- {
- writer.Write(buffer, 0, readCount);
- readCount = responseStream.Read(buffer, 0, bufferSize);
- }
- }
- reader.Close();
- response.Close();
SFTP
First you must download and compile the SSH.Net library. Add the compiled .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();
- }