Creating a byte array from a stream

后端 未结 16 2954
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-21 23:22

What is the prefered method for creating a byte array from an input stream?

Here is my current solution with .NET 3.5.

Stream s;
byte[] b;

using (         


        
16条回答
  •  说谎
    说谎 (楼主)
    2020-11-21 23:36

    just my couple cents... the practice that I often use is to organize the methods like this as a custom helper

    public static class StreamHelpers
    {
        public static byte[] ReadFully(this Stream input)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                input.CopyTo(ms);
                return ms.ToArray();
            }
        }
    }
    

    add namespace to the config file and use it anywhere you wish

提交回复
热议问题