Sponsored Ad

Wednesday, January 6, 2010

How to Download File From FTP Server in C#, C# Source Code to Download File from FTP

Thanks to the  .NET Framework  2.0 , all the basic functionality to access an FTP server is already built into C #.

This method downloads file from FTP and add date time to new File name.

 

Key points

  • Each application will require an object attached to its NetworkCredentials Credentials property. This tells the application how to authenticate against the FTP server.
  • The URI provided in order of application must include the name of the file you need to load or unload. For example, if we are downloading the file "data.xml" of abc.ftp.com our ftp://abc.ftp.com/data.xml URI.
  • You need to be familiar with the Stream object. We will use this object, both upload and download files.
  • These can be simple, when I started moving files to / from FTP servers do not understand some of these concepts, so I thought it would be important! Now go to the code.

Steps to follow


   1. Create ftp object
   2. Login to ftp server using userid and password
   3. Set keepalive property true to multiple use of same instance 
   4. Use binary method to upload data
   5. Set action to check the date and time of ftp file
   5. Call our used defined method to download file
   6. Open file in filesteam
   7. Read the FTP file in buffer
   8. Save file at local drive

 

Code Snippet
  1. public void DownloadFile(string ftppath, string destfile,string strFileName)
  2.     {
  3.         try
  4.         {
  5.             //create ftp object
  6.             FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftppath);
  7.             //userid and password for the ftp server
  8.             ftp.Credentials = new NetworkCredential("ftp_username", "ftp_password");
  9.             //Set keepalive property true to multiple use of same instance  
  10.             ftp.KeepAlive = true;
  11.             //use binary method to upload data
  12.             ftp.UseBinary = true;
  13.             //set action to check the date and time of ftp file
  14.             ftp.Method = WebRequestMethods.Ftp.GetDateTimestamp;
  15.              FtpWebResponse response = (FtpWebResponse)ftp.GetResponse();
  16.             //recieve ftp file date time
  17.             DateTime modification = response.LastModified;
  18.  
  19.             // Download the data
  20.              string sBackupFileName = strFileName + modification.ToString("MM_dd_yyyy") + "_" + modification.Hour.ToString() + "_" + modification.Minute.ToString() + "_" + modification.Second.ToString() + ".xml";
  21.             //download file in byte , for this call another user defined method given in this post
  22.             byte[] Data = DownloadData(ftppath);
  23.  
  24.             // Save the data to disk
  25.             FileStream fs = new FileStream(destfile + sBackupFileName, FileMode.Create);
  26.  
  27.             fs.Write(Data, 0, Data.Length);
  28.             //close the file stream
  29.             fs.Close();
  30.         }
  31.         catch (Exception ex)
  32.         {
  33.             //exception handling
  34.             throw;
  35.         }
  36.  
  37.     }

 

Code Snippet
  1. //user define method to download data from FTP
  2.    public byte[] DownloadData(string path)
  3.   {
  4.       // Get the object used to communicate with the server.
  5.       WebClient request = new WebClient();
  6.  
  7.       // Logon to the server using username + password
  8.       ftp.Credentials = new NetworkCredential("ftp_username", "ftp_password");
  9.  
  10.       return request.DownloadData(path);
  11.   }

 

How to call method:

DownloadFile("ftp://abc.ftp.com/data.xml", Server.MapPath("/abc/Documents/"), "new_file");

0 comments:

Post a Comment

Sponsored Ad

More Related Articles

Website Update

Followers