How can I get file from FTP (using C#)?

26,028

Solution 1

Take a look at How to: Download Files with FTP or downloading all files in directory ftp and c#

 // Get the object used to communicate with the server.
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
            request.Method = WebRequestMethods.Ftp.DownloadFile;

            // This example assumes the FTP site uses anonymous logon.
            request.Credentials = new NetworkCredential ("anonymous","[email protected]");

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();

            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);
            Console.WriteLine(reader.ReadToEnd());

            Console.WriteLine("Download Complete, status {0}", response.StatusDescription);

            reader.Close();
            reader.Dispose();
            response.Close();  

Edit If you want to rename file on FTP Server take a look at this Stackoverflow question

Solution 2

Easiest way

The most trivial way to download a binary file from an FTP server using .NET framework is using WebClient.DownloadFile.

It takes an URL to the source remote file and a path to the target local file. So you can use a different name for the local file, if you need that.

WebClient client = new WebClient();
client.Credentials = new NetworkCredential("username", "password");
client.DownloadFile(
    "ftp://ftp.example.com/remote/path/file.zip", @"C:\local\path\file.zip");

Advanced options

If you need greater control, that WebClient does not offer (like TLS/SSL encryption, ASCII mode, active mode, etc), use FtpWebRequest. Easy way is to just copy an FTP response stream to FileStream using Stream.CopyTo:

FtpWebRequest request =
    (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip");
request.Credentials = new NetworkCredential("username", "password");
request.Method = WebRequestMethods.Ftp.DownloadFile;

using (Stream ftpStream = request.GetResponse().GetResponseStream())
using (Stream fileStream = File.Create(@"C:\local\path\file.zip"))
{
    ftpStream.CopyTo(fileStream);
}

Progress monitoring

If you need to monitor a download progress, you have to copy the contents by chunks yourself:

FtpWebRequest request =
    (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip");
request.Credentials = new NetworkCredential("username", "password");
request.Method = WebRequestMethods.Ftp.DownloadFile;

using (Stream ftpStream = request.GetResponse().GetResponseStream())
using (Stream fileStream = File.Create(@"C:\local\path\file.zip"))
{
    byte[] buffer = new byte[10240];
    int read;
    while ((read = ftpStream.Read(buffer, 0, buffer.Length)) > 0)
    {
        fileStream.Write(buffer, 0, read);
        Console.WriteLine("Downloaded {0} bytes", fileStream.Position);
    }
}

For GUI progress (WinForms ProgressBar), see:
FtpWebRequest FTP download with ProgressBar


Downloading folder

If you want to download all files from a remote folder, see
C# Download all files and subdirectories through FTP.

Share:
26,028
revolutionkpi
Author by

revolutionkpi

Updated on August 05, 2022

Comments

  • revolutionkpi
    revolutionkpi over 1 year

    Now I know how to copy files from one directory to another, this is really simple.

    But now I need to do the same with files from FTP server. Can you give me some example how to get file from FTP while changing its name?

  • Xcalibur37
    Xcalibur37 over 12 years
    I would enclose your reader in a using block for proper disposal. Otherwise, add reader.Dispose() at the end.
  • Xcalibur37
    Xcalibur37 over 12 years
    I know this is a copy / paste of the MS content, but it's still not cleanly written.
  • revolutionkpi
    revolutionkpi over 12 years
    I understand that I need to save file in my local dict and only then - rename it? but can I do it directly?
  • Haris Hasan
    Haris Hasan over 12 years
    You want to rename file that exists on FTP?
  • revolutionkpi
    revolutionkpi over 12 years
    I don't need to rename file on ftp, but I need to save file that I get from ftp with new name
  • Haris Hasan
    Haris Hasan over 12 years
    Just rename the file once you have saved it. You can use File.Copy to rename file. First Copy/paste it with new name and and then delete the original file