Tuesday, September 14, 2010

Copy Files from FTP Server to Local or any other File System Server

Have you ever tried or worked on FTP files and you want to Copy them to specified drive of a server and then make some operations to it..?
If No, and Now you want to do the same or want to keep it for the future then this post is best for you.
I would recomment you to add it to your favorites for Future reference.

For simplicity, I will use Local Computer as FTP Server.
Step1: Create a FTP Folder. go to My Compute  then C:\inetput\ftproot\ and then create a Folder which will be treated as FTP folder. Note: IIS should be installed.
Step2: Save any TEXT File let say  basefile.txt which has some text.
Step3: Open the Visual Studio 2005/8/10 add a Console application.
Step4: Add a .cs File and rename the file as FTPFileTransfer.cs
Step5 Add Following code.


        public class  FTPFileTransfer
        {



           public FtpStatusCode Download(string destinationFile, Uri downloadUri, string userName, string password)
           {
               try
               {
                   // Check if the URI is and FTP site
                   if (downloadUri.Scheme != Uri.UriSchemeFtp)
                   {
                       throw new ArgumentException("Invalid FTP site");
                   }
                   // Set up the request
                   FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(downloadUri);
                   // Credentials for FTP URI
                   ftpRequest.Credentials =
                   new NetworkCredential(userName, password);
                   // Set method as file download.
                   ftpRequest.Method =
                   WebRequestMethods.Ftp.DownloadFile;
                   // get the response object
                   FtpWebResponse  ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
                   Stream stream = null;
                   StreamReader reader = null;
                   StreamWriter writer = null;
                   // get the file as a stream from the response
                      object and write it as a file stream to the local disk
                   try
                   {
                       stream = ftpResponse.GetResponseStream();
                       reader = new StreamReader(stream, Encoding.UTF8);
                       writer = new StreamWriter(destinationFile, false);
                       writer.Write(reader.ReadToEnd());
                       return ftpResponse.StatusCode;
                   }
                   finally
                   {
                       // Close streams
                       stream.Close();
                       reader.Close();
                       writer.Close();
                   }
               }
               catch (Exception ex)
               {
                   throw ex;
               }
           }
        }


Now you can create the object of this class and Call Download Method.

Please put your comments about the article..

No comments:

Post a Comment