using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Net; using System.IO; namespace FTPTransfer { public partial class Form1 : Form { public string sFTPServer = "ftp.domain.com"; public string sFTPUser = "ftpuser"; public string sFTPPass = "ftppassword"; public Form1 () { InitializeComponent(); } private void Form1_Load (object sender, EventArgs e) { DownloadFileFTP("/filename.pdf", @"C:Temp", true); } private void DownloadFileFTP (string sFTPFilePath, string sDownloadFilePath, bool bKeepFileName) { string ftpfullpath = "ftp://" + sFTPServer + sFTPFilePath; using (WebClient request = new WebClient()) { request.Credentials = new NetworkCredential(sFTPUser, sFTPPass); byte[] fileData = request.DownloadData(ftpfullpath); if (bKeepFileName == true) { sDownloadFilePath += sFTPFilePath; } using (FileStream file = File.Create(sDownloadFilePath)) { file.Write(fileData, 0, fileData.Length); file.Close(); } MessageBox.Show("Download Complete"); request.Dispose(); } } } }
Reference: http://stackoverflow.com/questions/2781654/ftpwebrequest-download-file
Last Updated on October 26, 2015