C # Upload entire directory using FTP

What I'm trying to do is upload a website using FTP in C # (C Sharp). Therefore, I need to upload all files and folders to a folder, preserving its structure. I use this FTP class: http://www.codeproject.com/Tips/443588/Simple-Csharp-FTP-Class for the actual download.

I came to the conclusion that I need to write a recursive method that goes through each subdirectory of the main directory and loads all the files and folders into it. This should make an exact copy of my FTP folder. The problem is ... I don’t know how to write such a method. I previously wrote recursive methods, but I'm new to the FTP part.

This is what I still have:

private void recursiveDirectory(string directoryPath) { string[] filePaths = null; string[] subDirectories = null; filePaths = Directory.GetFiles(directoryPath, "*.*"); subDirectories = Directory.GetDirectories(directoryPath); if (filePaths != null && subDirectories != null) { foreach (string directory in subDirectories) { ftpClient.createDirectory(directory); } foreach (string file in filePaths) { ftpClient.upload(Path.GetDirectoryName(directoryPath), file); } } } 

But that is far from all, and I do not know how to proceed. I'm sure more than I need to know this! Thanks in advance.

Oh and ... It would be nice if he reported on his progress too :) (I use the progress bar)

EDIT: Perhaps it was unclear ... How to download a directory, including all subdirectories and files from FTP?

+8
c # directory upload recursion ftp
source share
3 answers

The problem is resolved! :)

Ok, so I managed to write a myslef method. If anyone needs this, feel free to copy:

 private void recursiveDirectory(string dirPath, string uploadPath) { string[] files = Directory.GetFiles(dirPath, "*.*"); string[] subDirs = Directory.GetDirectories(dirPath); foreach (string file in files) { ftpClient.upload(uploadPath + "/" + Path.GetFileName(file), file); } foreach (string subDir in subDirs) { ftpClient.createDirectory(uploadPath + "/" + Path.GetFileName(subDir)); recursiveDirectory(subDir, uploadPath + "/" + Path.GetFileName(subDir)); } } 

This works very well :)

+11
source share

I wrote an FTP section and also wrapped it in a WinForms user control. You can see my code in the article "FtpClient Class and WinForm Management" .

+4
source share

If you do this for the sake of pleasure or self-improvement, use the commercial module. I can recommend one of Chilkat , but I'm sure there are others.

Note. . I am sure this answers the indicated problem. What I'm trying to do is upload a website using FTP in C # (C Sharp).

0
source share

All Articles