0

我有以下代码;

/// <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 中提取整个文件——它似乎在文件末尾完成下载之前就被切断了。

我可以添加什么来延长它允许检索数据的时间吗?

谢谢,

大卫

4

1 回答 1

0

而不是responseStream.Peek,使用下面的代码(您也可以Thread.Sleep安全地删除)

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

    list.Add(line);
}
于 2012-11-20T10:31:21.713 回答