Home >Backend Development >C++ >How to Write a Byte[] Array to a File in C#?

How to Write a Byte[] Array to a File in C#?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2025-01-05 22:16:44583browse

How to Write a Byte[] Array to a File in C#?

Writing Byte[] Array to a File in C#

A scenario arises where a Byte[] array representing a complete file needs to be written to a file in C#. How can this be achieved?

The FileStream class doesn't directly accept a byte array as an argument. However, the File class offers a straightforward solution:

Solution:

Based on the premise of writing a complete file from a Byte[] array, the optimal approach is to utilize the File.WriteAllBytes method:

File.WriteAllBytes("file_path.ext", byte_array);

Documented Here:

[System.IO.File.WriteAllBytes - MSDN](https://docs.microsoft.com/en-us/dotnet/api/system.io.file.writeallbytes?view=net-6.0)

The above is the detailed content of How to Write a Byte[] Array to a File in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn