Click here to Skip to main content
11,802,389 members (29,768 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# .NET
How do I download an entire folder (including subfolders) from a FTP server?
Posted 17-Dec-09 20:26pm
Edited 17-Dec-09 21:27pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

<img style="float: left;" alt="download" src="images/M_images/download.png" width="28" height="29" />Download the source code -423 KB

Today I put a sample program to manage your FTP
Perhaps all of you with many programs to work with FTP or have seen much of you need to have transferred the file to the server or servers to receive and manage the server.
It can create folders, manage and delete files and folders, upload and download files to your will.

Familiar with the concept for "FTP" and the launch method, click here

To view original size Click on the photos.

<img alt="Software_FTP" src="images/FtpClient/Software_FTP.jpg" width="511" height="346" />
<img alt="View_FTP" src="images/FtpClient/View_FTP.jpg" width="517" height="252" />

The software language is written in C # 2008.
I plan to have written part of office automation and felt that's too difficult to copy and upload in a network environment are fully software compatible with Windows Server - XP and 7 are by all FTP Microsoft and all FTP installed on the data center support.

Comment code:
1 - Uploading a file:

 private void Btn_UploadFTP_Click(object sender, EventArgs e)
   string _tempServerPath = Txt_ServerPath.Text + 
           "\\" + Path.GetFileName(Txt_LocalPath_Upload.Text);
   ftPclient1.FTPclient_SetInfo(Txt_Domain.Text, Txt_User.Text, Txt_Pass.Text);
   ftPclient1.Upload(Txt_LocalPath_Upload.Text, _tempServerPath);
public bool Upload(FileInfo fi, [Optional, DefaultParameterValue("")] string targetFilename)
  string str;
  bool _Lvr=true;
  if (targetFilename.Trim() == "")
       str = this.CurrentDirectory + fi.Name;
  else if (targetFilename.Contains("/"))
       str = this.AdjustDir(targetFilename);
        str = this.CurrentDirectory + targetFilename;
   string uRI = this.Hostname + str;
   FtpWebRequest request = this.GetRequest(uRI);
   request.Method = "STOR";
   request.UseBinary = true;
   request.ContentLength = fi.Length;
   byte[] array = new byte[0x800];
   using (FileStream stream = fi.OpenRead())
          using (Stream stream2 = request.GetRequestStream())
             int num;
                  num = stream.Read(array, 0, 0x800);
                  stream2.Write(array, 0, num);
                  CountProcesses = 
                             Math.Round((decimal)(stream.Position * 100) / stream.Length));
                while (num >= 0x800);
           catch (Exception exception1)
             _Lvr = false;
              Exception exception = exception1;
   request = null;
   return _Lvr;
2 - Download the file:

private void button4_Click(object sender, EventArgs e)
   string _Source_DownloadPath = Txt_ServerPath.Text + "\\" + Txt_FileNameDownload.Text;
   ftPclient1.FTPclient_SetInfo(Txt_Domain.Text, Txt_User.Text, Txt_Pass.Text);
   ftPclient1.Download(_Source_DownloadPath, Txt_LocalPath_Download.Text, false);

3 - Display files:

private void button5_Click(object sender, EventArgs e)
    Txt_View_FtpFile.Text = string.Empty; ;
    ftPclient1.FTPclient_SetInfo(Txt_Domain.Text, Txt_User.Text, Txt_Pass.Text);
    List<string> _listFile = ftPclient1.ListDirectory("//"+Txt_ServerPath.Text);
    for (int i = 0; i < _listFile.Count; i++)
       Txt_View_FtpFile.Text += _listFile[i].ToString() + Environment.NewLine;

Sandeep Mewara at 28-Apr-11 9:50am
Please stop posting it as an answer to every FTP question.

As we said, please just post it as a Tip/Trick and it will help others in future.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Recursion. You'd get a list of files and folders from the FTP site, so download the files, search the folders, and repeat.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 475
1 KrunalRohit 366
2 F-ES Sitecore 295
3 Afzaal Ahmad Zeeshan 230
4 CHill60 220
0 OriginalGriff 2,950
1 Maciej Los 1,910
2 KrunalRohit 1,862
3 CPallini 1,645
4 Richard MacCutchan 1,157

Advertise | Privacy | Mobile
Web01 | 2.8.151002.1 | Last Updated 28 Apr 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100