C# || How To Save, Open & Read File As A Byte Array & Memory Stream Using C#
The following is a module with functions which demonstrates how to save, open and read a file as a byte array and memory stream using C#.
1. Read File – Byte Array
The example below demonstrates the use of ‘Utils.Methods.ReadFile‘ to read a file as a byte array.
1 2 3 4 5 6 |
// Byte Array var path = $@"path-to-file\file.extension"; var fileByteArray = Utils.Methods.ReadFile(path); // Do something with the byte array |
2. Read File – Memory Stream
The example below demonstrates the use of ‘Utils.Methods.ReadFile‘ to read a file as a memory stream.
1 2 3 4 5 6 |
// Memory Stream var path = $@"path-to-file\file.extension"; using (var fileMS = new System.IO.MemoryStream(Utils.Methods.ReadFile(path))) { // Do something with the stream } |
3. Save File – Byte Array
The example below demonstrates the use of ‘Utils.Methods.SaveFile‘ to save the contents of a byte array to a file.
1 2 3 4 5 6 |
// Byte Array var path = $@"path-to-file\file.extension"; byte[] bytes; Utils.Methods.SaveFile(path, bytes); |
4. Save File – Memory Stream
The example below demonstrates the use of ‘Utils.Methods.SaveFile‘ to save the contents of a memory stream to a file.
1 2 3 4 5 6 |
// Memory Stream var path = $@"path-to-file\file.extension"; System.IO.MemoryStream fileMS; Utils.Methods.SaveFile(path, fileMS.ToArray()); |
5. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
// ============================================================================ // Author: Kenneth Perkins // Date: May 8, 2021 // Taken From: http://programmingnotes.org/ // File: Utils.cs // Description: Handles general utility functions // ============================================================================ using System; namespace Utils { public static class Methods { /// <summary> /// Reads a file at the given path into a byte array /// </summary> /// <param name="filePath">The path of the file to be read</param> /// <param name="shareOption">Determines how the file will be shared by processes</param> /// <returns>The contents of the file as a byte array</returns> public static byte[] ReadFile(string filePath, System.IO.FileShare shareOption = System.IO.FileShare.None) { byte[] fileBytes; using (var fs = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read, shareOption)) { fileBytes = new byte[fs.Length]; fs.Read(fileBytes, 0, fileBytes.Length); } return fileBytes; } /// <summary> /// Saves a file at the given path with the contents of the byte array /// </summary> /// <param name="filePath">The path of the file to be saved</param> /// <param name="fileBytes">The byte array containing the file contents</param> /// <param name="shareOption">Determines how the file will be shared by processes</param> public static void SaveFile(string filePath, byte[] fileBytes, System.IO.FileShare shareOption = System.IO.FileShare.None) { using (var fs = new System.IO.FileStream(filePath, System.IO.FileMode.Create, System.IO.FileAccess.Write, shareOption)) { fs.Write(fileBytes, 0, fileBytes.Length); } } } }// http://programmingnotes.org/ |
6. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
// ============================================================================ // Author: Kenneth Perkins // Date: May 8, 2021 // Taken From: http://programmingnotes.org/ // File: Program.cs // Description: The following demonstrates the use of the Utils Namespace // ============================================================================ using System; using System.Diagnostics; public class Program { static void Main(string[] args) { try { var path = $@"path-to-file\file.extension"; var fileByteArray = Utils.Methods.ReadFile(path); Utils.Methods.SaveFile(path, fileByteArray); } catch (Exception ex) { Display(ex.ToString()); } finally { Console.ReadLine(); } } static void Display(string message) { Console.WriteLine(message); Debug.Print(message); } }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Leave a Reply