how to convert System.IO.Stream into string and then back to System.IO.Stream

SOF User picture SOF User · Nov 30, 2010 · Viewed 71.8k times · Source

I have property of Stream type

public System.IO.Stream UploadStream { get; set; }

How can I convert it into a string and send on to other side where I can again convert it into System.IO.Stream?

Answer

Darin Dimitrov picture Darin Dimitrov · Nov 30, 2010

I don't know what do you mean by converting a stream to a string. Also what's the other side?

In order to convert a stream to a string you need to use an encoding. Here's an example of how this could be done if we suppose that the stream represents UTF-8 encoded bytes:

using (var reader = new StreamReader(foo.UploadStream, Encoding.UTF8))
{
    string value = reader.ReadToEnd();
    // Do something with the value
}