Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
How to connect FTP Server Through C# and download the files from FTP site
Posted
Comments
[no name] 19-Jan-15 8:03am    
If you dont mind using a 3rd library, you can try http://www.componentpro.com/ftp.net/. This is an example of downloading files from an FTP server: http://www.componentpro.com/doc/ftp/Downloading-multple-files-and-directories.htm

Here is the sample function that you need to modify according your requirement.
C#
public static List<periodicalfile> DownloadFTPFiles(string ftpAddress, string UserName, string Password)
{
    FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(ftpAddress);

    try
    {
        reqFTP.UsePassive = true;
        reqFTP.UseBinary = true;
        reqFTP.KeepAlive = false;
        reqFTP.Credentials = new NetworkCredential(UserName, Password);
        reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
        FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();

        Stream responseStream = response.GetResponseStream();
        List<string> files = new List<string>();
        StreamReader reader = new StreamReader(responseStream);
        while (!reader.EndOfStream)
            files.Add(reader.ReadLine());

        reader.Close();
        responseStream.Dispose();
        List<periodicalfile> lstFiles = new List<periodicalfile>();
        //Loop through the resulting file names.
        string ftpPath = string.Empty;
        foreach (var fileName in files)
        {
            var parentDirectory = "";
            PeriodicalFile lstFile = new PeriodicalFile();
            //If the filename has an extension, then it actually is 
            //a file            
            if (fileName.Contains(".zip"))
            {
                ftpPath = ftpAddress + fileName;
                lstFile.FTPFileName = ftpPath;
                listAllFTPFolder.Add(lstFile);
            }
            else
            {
                //If the filename has no extension, then it is just a folder. 
                //Run this method again as a recursion of the original:
                parentDirectory += fileName + "/";
                try
                {
                    DownloadFTPFiles(ftpAddress + "/" + parentDirectory, UserName, Password);
                }
                catch (Exception ex)
                {
                    ErrorLog.WriteLog("ftpFileProcessing", "DownloadFTPFiles(Else)", ex.Message, ex.StackTrace, "");
                }
            }
        }
    }
    catch (Exception excpt)
    {
        reqFTP.Abort();
        ErrorLog.WriteLog("ftpFileProcessing", "DownloadFTPFiles", excpt.Message, excpt.StackTrace, "");
    }
    return listAllFTPFolder;
}
 
Share this answer
 
v2
Example from MSDN, Hope this helps.
C#
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://www.contoso.com/test.htm");
            request.Method = WebRequestMethods.Ftp.DownloadFile;

            // 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);
            Console.WriteLine(reader.ReadToEnd());

            Console.WriteLine("Download Complete, status {0}", response.StatusDescription);
    
            reader.Close();
            response.Close();  
        }
    }
}
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900