Monday, March 16, 2009

C#: Convert Non-Seekable Stream to Byte Array

byte[] ConvertNonSeekableStreamToByteArray(Stream NonSeekableStream)
{
MemoryStream ms = new MemoryStream();
byte[] buffer = new byte[1024];
int bytes;
while ((bytes = NonSeekableStream.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, bytes);
}
byte[] output = ms.ToArray();
return output;
}

6 comments:

  1. i've see the post of your blog... compliments, are very usefull

    ReplyDelete
  2. hello sir , i want to ask can we edit the contents of the array in C#..I m reading the file using System.IO.File.ReadAllLines method, & my file contents are in format
    1^^^^Sonia^^^^Delhi
    2^^^^Ritu^^^^Gurgaon

    Suppose mine address is change to New Delhi from delhi,So i want to replace the contents in array,& then write all the array contents using System.IO.File.WriteAllLines.....Can u plz help me out...Plz send me the soln on mine site sonia.sardana@yahoo.co.in,
    I will be greatly appreciated..

    ReplyDelete
  3. YIPPEEEEEE!!!

    Thank you, thank you, thank you! If you're ever in Winnipeg, I'll buy you a beer!

    ReplyDelete
  4. Hi thank you very much for your post. It helped me alot. But i have one problem.

    My webresponsestream has nearly 10MB data. When we executing this method it is taking very huge time.

    Is there any solution to reduce the time.

    Please help me out : vsnaidu99@gmail.com

    ReplyDelete

Please use your common sense before making a comment, and I truly appreciate your constructive criticisms.