title: How to save downloaded files in MemoryStream when using SSH.NET
body: <p>I am using SSH.NET library to download files. I want to save the downloaded file as a file in memory, rather than a file on disk but it is not happening.</p>
  
  <p>This is my code which works fine:</p>
  
  <pre class="lang-cs prettyprint-override"><code>using (var sftp = new SftpClient(sFTPServer, sFTPPassword, sFTPPassword))
  {
      sftp.Connect();                    
  
      sftp.DownloadFile("AFile.txt", System.IO.File.Create("AFile.txt"));
      sftp.Disconnect();
  }
  </code></pre>
  
  <p>and this is the code which doesn't work fine as it gives 0 bytes stream.</p>
  
  <pre class="lang-cs prettyprint-override"><code>using (var sftp = new SftpClient(sFTPServer, sFTPPassword, sFTPPassword))
  {
      sftp.Connect();
  
      System.IO.MemoryStream mem = new System.IO.MemoryStream();
      System.IO.TextReader textReader = new System.IO.StreamReader(mem);
  
      sftp.DownloadFile("file.txt", mem);                    
      System.IO.TextReader textReader = new System.IO.StreamReader(mem);
      string s = textReader.ReadToEnd(); // it is empty
      sftp.Disconnect();
  }
  </code></pre>