Exemple #1
0
 /* Download File */
 public void Download(string localFile, string remoteFile)
 {
     try
     {
         /* Create an FTP Request */
         FtpRequest = (FtpWebRequest)FtpWebRequest.Create(InterlocutorAddress + "/" + remoteFile);
         /* Log in to the FTP Server with the User Name and Password Provided */
         FtpRequest.Credentials = new NetworkCredential(UserName, Password);
         LogInToInterlocutor(InterlocutorAddress);
         /* When in doubt, use these options */
         FtpRequest.UseBinary  = true;
         FtpRequest.UsePassive = true;
         FtpRequest.KeepAlive  = true;
         /* Specify the Type of FTP Request */
         FtpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
         /* Establish Return Communication with the FTP Server */
         FtpResponse = (FtpWebResponse)FtpRequest.GetResponse();
         /* Get the FTP Server's Response Stream */
         StartDownloadData(localFile, remoteFile, InterlocutorAddress);
         FtpStream = FtpResponse.GetResponseStream();
         /* Open a File Stream to Write the Downloaded File */
         FileStream localFileStream = new FileStream(localFile, FileMode.Create);
         /* Buffer for the Downloaded Data */
         byte[] byteBuffer = new byte[BufferSize];
         int    bytesRead  = FtpStream.Read(byteBuffer, 0, BufferSize);
         /* Download the File by Writing the Buffered Data Until the Transfer is Complete */
         try
         {
             while (bytesRead > 0)
             {
                 localFileStream.Write(byteBuffer, 0, bytesRead);
                 bytesRead = FtpStream.Read(byteBuffer, 0, BufferSize);
             }
         }
         finally
         { /* Resource Cleanup */
             localFileStream.Close();
             EndDownloadData(localFile, remoteFile, InterlocutorAddress);
         }
     }
     finally
     {
         FtpStream.Close();
         FtpResponse.Close();
         FtpRequest = null;
     }
 }