2

I am looking to download all the files in ftp to my local folder.All the files should be deleted in ftp once downloaded to local drive.

From the below code

  1. I can download only a file from ftp where I am not expecting

  2. I need to place all the files in a folder but not in the name of local file name.

My code:

using (WebClient ftpClient = new WebClient())
{
   ftpClient.Credentials = new System.Net.NetworkCredential("ftpusername", "ftp pass");
   ftpClient.DownloadFile("ftp://ftpdetails.com/dec.docx",@"D:\\Mainfolder\test.docx");
}

From the above code, i can download a file and place it in the name of file only..Where I have so many files to download from ftp and place it in a local folder..Any suggestions very much thankful.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Cherry
  • 675
  • 3
  • 10
  • 28
  • See [How to List Directory Contents with FTP in C#?](http://stackoverflow.com/questions/3298922/how-to-list-directory-contents-with-ftp-in-c). – CodeCaster Dec 11 '13 at 11:56
  • Your question is too broad and you don't explain which specific part you're having trouble with. You want to 1) list FTP directory contents and for each file in the list: 2) download, 3) write locally, 4) delete from FTP. See the question I linked as a duplicate for step 1, you must be able to figure the rest out from there. – CodeCaster Dec 11 '13 at 11:58

2 Answers2

5

Here's an example of using the FTPWebResponse to get a list of file names from a directory:

using System;
using System.IO;
using System.Net;
using System.Text;

namespace Examples.System.Net
{
    public class WebRequestGetExample
    {
        public static void Main()
        {
            // Get the object used to communicate with the server.
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.funet.fi/pub/standards/RFC/");
            request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

            // 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);

            while (!reader.EndOfStream)
            {
                String filename =  reader.ReadLine();
                Console.WriteLine(filename);
                //you now have the file name, you can use it to download this specific file


            }

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

            reader.Close();
            response.Close();
        }
    }
}

You can then use this list to download each file. Note that if you have a lot of files to download, you may want to look into asyncronous downloading to speed things up - but I would get this working first before you attempt to implement any async stuff.

Paul Coghill
  • 667
  • 6
  • 27
  • 2
    Note that "WebRequestMethods.Ftp.ListDirectoryDetails" also returns the date and the size of the files. With "WebRequestMethods.Ftp.ListDirectory", you just receive the file name, so no need to parse it :-) – Fred Mauroy May 18 '16 at 19:57
  • 1
    Paul, can you please give an example of how to use this list to download files? – dragy Jul 04 '17 at 10:29
1

I don't think WebClient is a valid FTP client. Use standard classes FtpWebRequest and FtpWebResponse instead.

Otherwise there are a few free C# ftp clients out there that will do the job.

DarkUrse
  • 2,084
  • 3
  • 25
  • 33