ila

ila

  • NA
  • 19.1k
  • 0

could not read the content downloaded from ftp server

Feb 9 2010 7:33 AM
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;

namespace DTSys.FTPDownloader
{
    public class FileDownloader
    {
        static void Main(string[] args)
        {
            string ftpServer = "ftp:*******.com";
            string userName = "*********.com";
            string password = "********";
            
            string destionationLocation = @"D:\project\ss\";
            try
            {
                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.GetFileName(fileName));
                            fDownloader.downloadFile(ftpServer, fileName, userName, password, fullPathDestionation);
                        }
                        Console.WriteLine("if statement closed");
                    }
                    else
                    {
                        Console.WriteLine("function is not called");
                    }
                
                Console.WriteLine("downloaded successfully");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }        
        public List<string> getFileList(string FTPAddress, string username, string password)
        {
            List<string> files = new List<string>();
            Console.WriteLine("getting filelist");
            try
            {              
                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)
            {
                Console.WriteLine(ex.Message);
            }
            Console.WriteLine("files list 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.Method = WebRequestMethods.Ftp.GetDateTimestamp;
                request.UsePassive = true;
                request.UseBinary = false;
                request.KeepAlive = false;                
                FtpWebResponse response = request.GetResponse() as FtpWebResponse;
                Stream reader = response.GetResponseStream();
                byte[] buffer = new byte[1024];                
                DateTime mod = response.LastModified;
                DateTime obj = DateTime.Today;
                int res1 = DateTime.Compare(mod, obj);                
                if (res1 != -1)
                {                    
                    Console.WriteLine("DOWNLOAD IF STATEMENT");
                    using (Stream streamFile = File.Create(destFile))
                    {
                        Console.WriteLine("USING STATEMENT");
                        while (true)
                        {
                            Console.WriteLine("WHILE STATEMENT");
                            int bytesRead = reader.Read(buffer, 0, buffer.Length);
                            if (bytesRead != 0)
                            {
                                Console.WriteLine("WHILE IF STATEMENT");
                                streamFile.Write(buffer, 0, bytesRead);
                            }
                            else
                            {
                                Console.WriteLine("WHILE ELSE STATEMENT");
                                break;
                            }
                        }
                    }
                }                
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.WriteLine("downloaded");
        }

    }
}
In the above code i can download the files which are created with current date.. but the contents of files could not be downloaded.. Because im getting the value of bytesRead as 0.. but there is content in tat file.. i dont know why.. anybody can help me?

Answers (2)