C#_Byte Array轉換成FileStream存出檔案

 首先將接收到的
byte array寫入至MemoryStream
再藉由MemoryStream寫入至FileStream(在初始時順帶指定存入至硬碟路徑)
最後記得沒用的Stream都要關閉以及釋放

byte[] fileData = restRes.RawBytes;
MemoryStream ms = new MemoryStream();
ms.Write(fileData, 0, fileData.Length);
ms.Seek(0, SeekOrigin.Begin);

FileStream fileStream = new FileStream(CurDProjectDir + @"\TestData\test.pdf", FileMode.Create);
ms.WriteTo(fileStream);
fileStream.Close();
fileStream.Dispose();
ms.Close();
ms.Dispose();






Ref:
Save and load MemoryStream to/from a file
https://stackoverflow.com/questions/8624071/save-and-load-memorystream-to-from-a-file

Convert a Byte Array to a Stream in C#
https://medium.com/@stevenscript/convert-a-byte-array-to-a-stream-in-c-840889a59174

How do I convert struct System.Byte byte[] to a System.IO.Stream object in C#?
https://stackoverflow.com/questions/4736155/how-do-i-convert-struct-system-byte-byte-to-a-system-io-stream-object-in-c/4736185

留言

這個網誌中的熱門文章

何謂淨重(Net Weight)、皮重(Tare Weight)與毛重(Gross Weight)

Architecture(架構) 和 Framework(框架) 有何不同?_軟體設計前的事前規劃的藍圖概念

經得起原始碼資安弱點掃描的程式設計習慣培養(五)_Missing HSTS Header