2011-06-13 57 views
4

我遇到了問題,問題是我可以下載ftp文件,但是我下載的文件沒有恢復設備和多部分文件下載,因爲有超過500個大文件MB的文件我無法汽車無下載文件,因爲我被切斷,並開始從開始下載我想我的代碼恢復工具,如果它得到斷開如何使用自動恢復功能下載ftp文件

我使用的代碼是

public string[] GetFileList() 
    { 
     string[] downloadFiles; 
     StringBuilder result = new StringBuilder(); 
     FtpWebRequest reqFTP; 
     try 
     { 
      reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/")); 
      reqFTP.UseBinary = true; 
      reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); 
      reqFTP.Method = WebRequestMethods.Ftp.ListDirectory; 
      WebResponse response = reqFTP.GetResponse(); 
      StreamReader reader = new StreamReader(response.GetResponseStream()); 
      //MessageBox.Show(reader.ReadToEnd()); 
      string line = reader.ReadLine(); 
      while (line != null) 
      { 
       result.Append(line); 
       result.Append("\n"); 
       line = reader.ReadLine(); 
      } 
      result.Remove(result.ToString().LastIndexOf('\n'), 1); 
      reader.Close(); 
      response.Close(); 
      //MessageBox.Show(response.StatusDescription); 
      return result.ToString().Split('\n'); 
     } 
     catch (Exception ex) 
     { 
      System.Windows.Forms.MessageBox.Show(ex.Message); 
      downloadFiles = null; 
      return downloadFiles; 
     } 
    } 


    private void Download(string filePath, string fileName) 
    { 
     FtpWebRequest reqFTP; 
     try 
     { 
      //filePath = <<The full path where the file is to be created.>>, 
      //fileName = <<Name of the file to be created(Need not be the name of the file on FTP server).>> 
      FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create); 

      reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileName)); 
      reqFTP.Method = WebRequestMethods.Ftp.DownloadFile; 
      reqFTP.UseBinary = true; 
      reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); 
      FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); 
      Stream ftpStream = response.GetResponseStream(); 
      long cl = response.ContentLength; 
      int bufferSize = 2048; 
      int readCount; 
      byte[] buffer = new byte[bufferSize]; 

      readCount = ftpStream.Read(buffer, 0, bufferSize); 
      while (readCount > 0) 
      { 
       outputStream.Write(buffer, 0, readCount); 
       readCount = ftpStream.Read(buffer, 0, bufferSize); 
      } 

      ftpStream.Close(); 
      outputStream.Close(); 
      response.Close(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
    } 

私人無效btnDownload_Click(對象發送者,EventArgs e) {

 FolderBrowserDialog fldDlg = new FolderBrowserDialog(); 
     if (txtUpload.Text.Trim().Length > 0) 
     { 
      if (fldDlg.ShowDialog() == DialogResult.OK) 
      { 
       Download(fldDlg.SelectedPath, txtUpload.Text.Trim()); 
      } 
     } 
     else 
     { 
      MessageBox.Show("Please enter the File name to download"); 
     } 
    } 

我想在我的代碼恢復設施,

你開始下載檢查是否存在之前,會有一個很大的升值,如果有人可以幫助我,

在此先感謝

回答

6

本地文件系統上的文件。如果存在,則獲取大小並將其用於FtpWebRequest對象的ContentOffset成員。不過,FTP服務器可能不支持該功能。

+0

它有一個簡歷設施 – 2011-06-13 13:46:36

+2

這是你需要回答的問題:) – 2011-06-13 13:54:09