How to save StreamReader contents in a string

How to save StreamReader contents in a string

I am trying to keep the contents StreamReaderin a string. Unfortunately, I am not allowed to save the content because the object seems to be lost (coming from an FTP server).

GERMAN error message: Auf das verworfene Objekt kann nicht zugegriffen werden. Objektname: "System.Net.Sockets.NetworkStream".

Error message ENGLISH: Access to the ban object is not available. Object Name: "System.Net.Sockets.NetworkStream".

StreamReader streamReader = new StreamReader(responseStream);
string text = streamReader.ReadToEnd();

Error from line 2.


Edit:

public void DownloadFileWithFtp()
    {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://XYZ.bplaced.net/Testumgebung/Texte/" + comboBox_DataPool.SelectedValue);
        request.Credentials = new NetworkCredential("BN", "PW");
        request.Method = WebRequestMethods.Ftp.DownloadFile;

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
        Stream responseStream = response.GetResponseStream();

        StreamReader streamReader = new StreamReader(responseStream);
        MessageBox.Show(streamReader.ReadToEnd());
        //textBoxText = streamReader.ReadToEnd();
        streamReader.Close();

        MessageBox.Show(response.StatusDescription);
        response.Close();
    }
+4
source share
1 answer

responseStream, GetResponseStream() FtpWebResponse, , CanSeek false.

. , ReadToEnd(), Seek(0,0). responseStream.Seek(0, 0); NotSupportedException.

. Using , Close():

public void DownloadFileWithFtp() {
  FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://XYZ.bplaced.net/Testumgebung/Texte/" + comboBox_DataPool.SelectedValue);
  request.Credentials = new NetworkCredential("BN", "PW");
  request.Method = WebRequestMethods.Ftp.DownloadFile;

  using(FtpWebResponse response = (FtpWebResponse)request.GetResponse()) {
    using(Stream responseStream = response.GetResponseStream()) {
      using(StreamReader streamReader = new StreamReader(responseStream)) {
        string content = streamReader.ReadToEnd();
        MessageBox.Show(content);
        textBox.Text = content;
      }
    }
    MessageBox.Show(response.StatusDescription);
  }
}
+1

All Articles