2012-11-20 87 views
0

我有以下代碼;FTP未檢索完整文件

/// <summary> 
/// Gets a file 
/// </summary> 
/// <param name="filename"> The filename to get. </param> 
/// <returns> A list, containing each line of the file </returns> 
public IEnumerable<string> GetFileLines(string filename) 
{ 
    var list = new List<string>(); 

    var uri = BuildUri(filename); 

    var request = (FtpWebRequest)WebRequest.Create(uri.Uri); 
    request.Method = WebRequestMethods.Ftp.DownloadFile; 

    using (var resp = (FtpWebResponse)request.GetResponse()) 
    { 
    using (var stream = resp.GetResponseStream()) 
    { 
     Thread.Sleep(10000); 
     if (stream != null) 
     { 
     using (var responseStream = new StreamReader(stream)) 
     { 
      // If we still have characters to process 
      while (responseStream.Peek() >= 0) 
      { 
      var line = responseStream.ReadLine(); 
      list.Add(line); 
      } 
     } 
     } 
    } 
    } 

    return list; 
} 

但是它不拉從FTP-整個文件似乎文件的末尾下載完成之前切斷。

有什麼我可以添加來延長它允許檢索數據的時間?

感謝,

大衛

+0

有任何錯誤,而讀書?您是否嘗試過在網絡瀏覽器中使用FTP地址手動獲取文件並檢查是否可以? –

回答

0

代替responseStream.Peek,下面的代碼使用(你也可以刪除Thread.Sleep安全)

string line; 
while((line = responseStream.ReadLine())!=null) 
{ 

    list.Add(line); 
}