How to download a file from ftp server?
public void Download(string filePath, string fileName) {
FtpWebRequest reqFTP; try
{
//filePath: The full path where the file is to be created.
//fileName: Name of the file to be createdNeed not name on
// the FTP server. name name()
FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create); string ftpServerIP = this.textBoxFtpServerIP.Text + this.textBoxForFilePath.Text; string ftpUserID = this.textBoxForUserID.Text; string ftpPassword = this.textBoxForPassword.Text; reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://"
+ ftpServerIP + "/" + fileName)); reqFTP.Method = WebRequestMethods.Ftp.DownloadFile; reqFTP.UseBinary = true; reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); Stream ftpStream = response.GetResponseStream(); long cl = response.ContentLength; int bufferSize = 2048; int readCount; byte[] buffer = new byte[bufferSize]; readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0) {
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Close();
outputStream.Close();
response.Close();
}
catch (Exception ex) {
MessageBox.Show(ex.Message); }
}