ila

ila

  • NA
  • 19.1k
  • 0

listing files of a directory from ftp server..

Feb 2 2010 4:43 AM
public class FileDownloader
    {
        static void Main(string[] args)
        {
            string ftpServer = "ftp://ftp.******.com";
            string userName = "*****";
            string password = "******";
            string destionationLocation = @"D:\project\ss";           
            FileDownloader fDownloader = new FileDownloader();           
            List<string> collectionOfFiles = fDownloader.getFileList(ftpServer, userName, password);
            if (collectionOfFiles != null)
            {
                Console.WriteLine("if statement");
                Console.WriteLine(collectionOfFiles);
                foreach (string fileName in collectionOfFiles)
                {
                    Console.WriteLine("for statment");                   
                    string fullPathDestionation = Path.Combine(destionationLocation, Path.GetFileNameWithoutExtension(fileName) + ".*");                   
                    fDownloader.downloadFile(ftpServer, fileName, userName, password, fullPathDestionation);
                }
                Console.WriteLine("if statement closed");
            }
            else
            {
                Console.WriteLine("function is not called");
            }
            Console.WriteLine("downloaded successfully");
        }       
        public List<string> getFileList(string FTPAddress, string username, string password)
        {
            List<string> files = new List<string>();
            Console.WriteLine("getting filelist");
            try
            {
                //Create FTP request
                FtpWebRequest request = FtpWebRequest.Create(FTPAddress) as FtpWebRequest;

                request.Method = WebRequestMethods.Ftp.ListDirectory;
                request.Credentials = new NetworkCredential(username, password);
                request.UsePassive = true;
                request.UseBinary = true;
                request.KeepAlive = false;

                FtpWebResponse response = request.GetResponse() as FtpWebResponse;
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(responseStream))
                    {
                        while (!reader.EndOfStream)
                        {
                            files.Add(reader.ReadLine());
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                // write to log
            }
            Console.WriteLine("fileslist got");
            return files;           
        }

        public void downloadFile(string FTPAddress, string filename, string username, string password, string destFile)
        {
            Console.WriteLine("Downloading files");
            try
            {               
                FtpWebRequest request = FtpWebRequest.Create(FTPAddress + filename) as FtpWebRequest;
                request.Method = WebRequestMethods.Ftp.DownloadFile;
                request.Credentials = new NetworkCredential(username, password);
                request.UsePassive = true;
                request.UseBinary = false;
                request.KeepAlive = false; //close the connection when done
                //Streams
                FtpWebResponse response = request.GetResponse() as FtpWebResponse;
                Stream reader = response.GetResponseStream();

                byte[] buffer = new byte[1024];
                using (Stream streamFile = File.Create(destFile))
                {
                    while (true)
                    {
                        int bytesRead = reader.Read(buffer, 0, buffer.Length);
                        if (bytesRead == 0)
                        {
                            break;
                        }
                        else
                        {
                            streamFile.Write(buffer, 0, bytesRead);

                        }
                    }
                }
            }
            catch (Exception ex)
            {
                // write to log
            }
            Console.WriteLine("downloaded");
        }
I tried the above code to list the files of directory from ftp server and then download them in c#.. but the following code does not execute.. i dont know why.. can anybody help for me?

foreach (string fileName in collectionOfFiles)
                {
                    Console.WriteLine("for statment");                   
                    string fullPathDestionation = Path.Combine(destionationLocation, Path.GetFileNameWithoutExtension(fileName) + ".*");                   
                    fDownloader.downloadFile(ftpServer, fileName, userName, password, fullPathDestionation);
                }

Answers (1)