4

How to: Save a StreamReader content in a string

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

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

Error message ENGLISH: There is no access to the castaway object. Object name: "System.Net.Sockets.NetworkStream".

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

The error is 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();
    }
sjantke
  • 605
  • 4
  • 9
  • 35
  • 2
    There's nothing wrong with the StreamReader here, it's just getting an exception back from the underlying stream. How are you constructing the responseStream in the first place? – dyson Jul 22 '14 at 20:18
  • Thanks. Please see the edited post with my complete code. – sjantke Jul 22 '14 at 20:23
  • 3
    Just some general advice from a fellow German: Uninstall all language packs. The English messages are more precise, it is easier to get help and professional development happens in English anyway. (The English message would have told you literally that you are accessing a disposed object. This fact is not recognizable to you due to mistranslation.) – usr Jul 22 '14 at 20:25
  • 1
    As a workaround, use `WebClient.DownLoadString()` to replace al that code with 1 line. – H H Jul 22 '14 at 20:30
  • Thanks for your help. I am going to uninstall all language packs and I am trying to use that workaround. However, my problem is not fixed. :-) – sjantke Jul 22 '14 at 20:45

1 Answers1

1

If you inspect the responseStream returned from the GetResponseStream() method of the FtpWebResponse, you will notice that the boolean property CanSeek is false.

Reading streams multiple times in this way would always result in an error. Before the next call to ReadToEnd() is done you should precede it with a call to Seek(0,0). However, in this case, a call to responseStream.Seek(0, 0); would result in an NotSupportedException being thrown.

Assigning the result once to an intermediate variable would work. Also use Using blocks rather than the Close() method:

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);
  }
}
Louis Somers
  • 2,560
  • 3
  • 27
  • 57