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

Now I know how to copy files from one directory to another, it is very simple.

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

+5
source share
2 answers

Take a look at How to upload files from FTP or upload all files to ftp and C # directory

// 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"," janeDoe@contoso.com "); 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 the file on an FTP server, take a look at this fooobar.com/questions/644686 / ...

+7
source

The easiest way

The easiest way to download a binary file from an FTP server using the .NET Framework is to use WebClient.DownloadFile .

It takes the path to the source remote file and the target local file. This way you can use a different name for the local file if you need it.

 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 settings

If you need more control that WebClient does not offer (e.g. TLS / SSL encryption , etc.), use FtpWebRequest . An easy way is to simply copy the 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 the progress of the download, you must copy the contents in parts:

 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 from ProgressBar


Download Folder

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

+5
source

Source: https://habr.com/ru/post/1388030/


All Articles