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
Originally Posted on May 21, 2014
Last Updated on October 26, 2015
Last Updated on October 26, 2015
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.