I am getting the result StreamReader
object.
I want to convert the result into byte[]
.
How can I convert StreamReader
to byte[]
?
Thanks
Just throw everything you read into a MemoryStream
and get the byte array in the end. As noted, you should be reading from the underlying stream to get the raw bytes.
var bytes = default(byte[]); using (var memstream = new MemoryStream()) { var buffer = new byte[512]; var bytesRead = default(int); while ((bytesRead = reader.BaseStream.Read(buffer, 0, buffer.Length)) > 0) memstream.Write(buffer, 0, bytesRead); bytes = memstream.ToArray(); }
Or if you don't want to manage the buffers:
var bytes = default(byte[]); using (var memstream = new MemoryStream()) { reader.BaseStream.CopyTo(memstream); bytes = memstream.ToArray(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With