What is the most efficient way to save a byte array as a file on disk in C#?

前端 未结 7 1161

Pretty simple scenario. I have a web service that receives a byte array that is to be saved as a particular file type on disk. What is the most efficient way to do this in C#?

相关标签:
7条回答
  • 2021-02-06 20:56

    And WriteAllBytes just performs

    using (FileStream stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read))
    {
        stream.Write(bytes, 0, bytes.Length);
    }
    

    BinaryWriter has a misleading name, it's intended for writing primitives as a byte representations instead of writing binary data. All its Write(byte[]) method does is perform Write() on the stream its using, in this case a FileStream.

    0 讨论(0)
提交回复
热议问题