How do I convert a Stream into a byte[] in C#? [duplicate]

The shortest solution I know:

using(var memoryStream = new MemoryStream())
{
  sourceStream.CopyTo(memoryStream);
  return memoryStream.ToArray();
}

Leave a Comment