C#中 Stream轉為byte[]


 

 

Stream轉為byte[],可能Stream的Read方法的count參數會因為byte數組長度太長超過int最大值報錯,目前我還沒找到解決方案。。。

 1         public static byte[] StreamToBytes(this Stream stream)
 2         {
 3             byte[] bytes = new byte[stream.Length];
 4             try
 5             {
 6                 stream.Read(bytes, 0, bytes.Length);
 7                 // 設置當前流的位置為流的開始
 8                 stream.Seek(0, SeekOrigin.Begin);
 9                 return bytes;
10             }
11             catch (Exception ex)
12             {
13                 LogUtil.Log.Error("將Stream轉為Byte[]時錯誤", ex);
14             }
15             return bytes;
16         }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM