0

So what I've tried to do is download multiple files in a directory on a FTP Server into a Local Directory, I've figured out how to download just one file, but I don't know how to download multiple files.

FtpWebRequest request =
(FtpWebRequest)WebRequest.Create(ip.Text + "/mods/");
                request.Credentials = new NetworkCredential(user.Text, pass.Text);
                request.Method = WebRequestMethods.Ftp.DownloadFile;

                using (Stream ftpStream = request.GetResponse().GetResponseStream())
                using (Stream fileStream = File.Create(directory.Text + @"\mods\"))
                {
                    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);
                    }

Whenever I use just the /mods/ directory, I get a error that says WebClient Exception.

Whenever I use /mods/example_file.txt it successfully downloads.

So my Goal with all this is, how can I download the entire /mods/ directory to my computer.

I have searched the internet and read multiple posts and I cannot seem to figure this out.

I am also a beginner in C# so most likely I am doing something wrong.

Rondell
  • 3
  • 4

1 Answers1

0

Possible duplicate of How to transfer multiple files from FTP server to local directory using C#?

To summarise : c# and .net framework (or any other framework) dosent change the underlying nature of FTP server and communication protocol. In FTP you do not have command that says 'GET EVERYTHING UNDER DIR X', instead you have to list the contents of a directory and depending upon your requirements you have to request specific files.

You can iterate through the contents of directory and list each file separately, if you are looking for more advance behaviour such as concurrency etc. then you can use framework specific features (asynchronous programming and multithreading etc.) to achieve that.

Amogh Sarpotdar
  • 544
  • 4
  • 15
  • I see the post you linked and tried it but no success. Only issue is that the files in the `/mods/` directory on the FTP changes pretty much everyday, so if I list each file separately then wouldn't I have to change it each time the files in the FTP changes? I'm sorry if I make no sense, I'm new with C# and FTP stuff so I get easily confused. – Rondell May 03 '20 at 19:12
  • Are your files so big that they take more than a day to download? If that is the case then you have a logical problem in process flow. For example, you may want the server process to generate files in a folder marked with date. Besides, there is no way to track changes on FTP server. The best you can do is rely on 'date created' of file on the destination. – Amogh Sarpotdar May 03 '20 at 19:35
  • What do you mean by *"no success"*? You then wrote that it actually works, only that it is too slow. So did you succeed with writing a code that download all files in a folder or not? Do you to download only new files instead? – Martin Prikryl May 04 '20 at 05:04