When I got the task to download the files from FTP with subdirectories, I searched many articles and many posts for the sample code but I get partially. i.e only download the file by giving end path, as shown below (FTPaddress/Directory/Sub-Dir1/Sub-Dir2/Sub-Dir3/Filename.zip) but I want to download the file by giving only this (FTPaddress/Directory). After the hours of work, I got the output. I want to share the code. I think, it will help the people while they struggle in this part.
Let's enter into the code.
- public void DownloadFile() {
- string dirpath = txtlocalpath.Text;
- try {
- FtpWebRequest ftpRequest = (FtpWebRequest) WebRequest.Create("ftp://xxx.xxx.xx.xx/"); // FTP Address
- ftpRequest.Credentials = new NetworkCredential(txtusername.Text, txtpassword.Text);
- ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
- FtpWebResponse response = (FtpWebResponse) ftpRequest.GetResponse();
- StreamReader streamReader = new StreamReader(response.GetResponseStream());
- List < string > directories = new List < string > ();
- string line = streamReader.ReadLine();
- while (!string.IsNullOrEmpty(line)) {
- directories.Add(line);
- line = streamReader.ReadLine();
- }
- using(WebClient ftpClient = new WebClient()) {
- ftpClient.Credentials = new NetworkCredential(txtusername.Text, txtpassword.Text);
- string[] filename = txtftpaddress.Text.Split('/');
- Filename = filename[filename.Length - 1];
- for (int k = 0; k <= filename.Length - 1; k++) {
- dirpath = dirpath + "\\" + filename[k];
- createdir(dirpath);
- }
- try {
- for (int i = 0; i <= directories.Count - 1; i++) {
- if (directories[i].Contains(".")) {
- string path = "ftp://xxx.xxx.xx.xx//" + txtftpaddress.Text + "/" + directories[i].ToString();
- ftpClient.DownloadFile(path, dirpath.ToString() + "\\" + directories[i].ToString());
- } else {
- string path = "ftp://xxx.xxx.xx.xx//" + txtftpaddress.Text + "/" + directories[i].ToString();
- string subdirpath = dirpath + "\\" + directories[i].ToString();
- createdir(subdirpath);
- string[] subdirectory = Return(path, txtusername.Text, txtpassword.Text);
- for (int j = 0; j <= subdirectory.Length - 1; j++) {
- string subpath = directories[i] + "/" + subdirectory[j];
- directories.Add(subpath);
- }
- }
- }
- } catch (WebException e) {
- String status = ((FtpWebResponse) e.Response).StatusDescription;
- MessageBox.Show(status.ToString());
- }
- }
- streamReader.Close();
- } catch (WebException l) {
- String status = ((FtpWebResponse) l.Response).StatusDescription;
- MessageBox.Show(status.ToString());
- }
- }
-
- public string[] Return(string filepath, string username, string password) {
- List < string > directories = new List < string > ();
- try {
- FtpWebRequest ftpRequest = (FtpWebRequest) WebRequest.Create(filepath);
- ftpRequest.Credentials = new NetworkCredential(username, password);
- ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
- FtpWebResponse response = (FtpWebResponse) ftpRequest.GetResponse();
- StreamReader streamReader = new StreamReader(response.GetResponseStream());
- string line = streamReader.ReadLine();
- while (!string.IsNullOrEmpty(line)) {
- directories.Add(line);
- line = streamReader.ReadLine();
- }
- } catch (WebException e) {
- String status = ((FtpWebResponse) e.Response).StatusDescription;
- System.Windows.Forms.MessageBox.Show(status.ToString());
- }
- return directories.ToArray();
- }
-
- public void createdir(string path) {
- if (!Directory.Exists(path)) {
- Directory.CreateDirectory(path);
- }
- }