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;
     }
 }
Exemple #2
0
        /* Get the Size of a File */
        public string GetFileSize(string fileName)
        {
            try
            {
                /* Create an FTP Request */
                FtpRequest = (FtpWebRequest)FtpWebRequest.Create(InterlocutorAddress + "/" + fileName);
                /* 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.GetFileSize;
                /* Establish Return Communication with the FTP Server */
                FtpResponse = (FtpWebResponse)FtpRequest.GetResponse();
                /* Establish Return Communication with the FTP Server */
                FtpStream = FtpResponse.GetResponseStream();
                /* Get the FTP Server's Response Stream */
                StreamReader ftpReader = new StreamReader(FtpStream);
                /* Store the Raw Response */
                string fileInfo = null;
                /* Read the Full Response Stream */
                try { while (ftpReader.Peek() != -1)
                      {
                          fileInfo = ftpReader.ReadToEnd();
                      }
                }
                finally
                {
                    /* Resource Cleanup */
                    ftpReader.Close();
                }

                /* Return File Size */
                return(fileInfo);
            }
            finally
            {
                FtpStream.Close();
                FtpResponse.Close();
                FtpRequest = null;
            }
        }
Exemple #3
0
 /* List Directory Contents in Detail (Name, Size, Created, etc.) */
 public string[] DirectoryListDetailed(string directory)
 {
     try
     {
         /* Create an FTP Request */
         FtpRequest = (FtpWebRequest)FtpWebRequest.Create(InterlocutorAddress + "/" + directory);
         /* 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.ListDirectoryDetails;
         /* Establish Return Communication with the FTP Server */
         FtpResponse = (FtpWebResponse)FtpRequest.GetResponse();
         /* Establish Return Communication with the FTP Server */
         FtpStream = FtpResponse.GetResponseStream();
         /* Get the FTP Server's Response Stream */
         StreamReader ftpReader = new StreamReader(FtpStream);
         /* Store the Raw Response */
         string directoryRaw = null;
         /* Read Each Line of the Response and Append a Pipe to Each Line for Easy Parsing */
         try { while (ftpReader.Peek() != -1)
               {
                   directoryRaw += ftpReader.ReadLine() + "|";
               }
         }
         finally
         {  /* Resource Cleanup */
             ftpReader.Close();
         }
         /* Return the Directory Listing as a string Array by Parsing 'directoryRaw' with the Delimiter you Append (I use | in This Example) */
         return(directoryRaw.Split("|".ToCharArray()));
     }
     finally
     {
         FtpStream.Close();
         FtpResponse.Close();
         FtpRequest = null;
     }
 }
Exemple #4
0
 private void SafeRelease(ref FtpStream obj)
 {
     if (obj != null)
     {
         try { obj.Close(); }
         catch { }
         obj = null;
     }
 }