How to convert Stream into String?

Posted by Poster under C# category on | Views : 58051
To convert Stream object into String, write following code in C#.

        Stream stream = GetStream();
byte[] bytes = new byte[stream.Length];
stream.Position = 0;
stream.Read(bytes, 0, (int)stream.Length);
string data = Encoding.ASCII.GetString(bytes); // this is your string

In VB.NET, write following code.

Dim tream As Stream = GetStream()
Dim bytes As Byte() = New Byte(tream.Length) {}
tream.Position = 0
tream.Read(bytes, 0, DirectCast(tream.Length, Integer))
Dim data As String = Encoding.ASCII.GetString(bytes) ' this is your string

You need to use System.IO and System.Text namespace to work with above code.

Comments or Responses

Login to post response