using System.IO; using System.Text; class Program { static void Main(string[] args) { using (FileStream fileStream = new FileStream("binarydata.bin", FileMode.Create)) { byte[] bytes = Encoding.UTF8.GetBytes("Hello World"); fileStream.WriteBe(bytes); } } }
using System.IO; class Program { static void Main(string[] args) { using (var fileStream = new FileStream("data.bin", FileMode.OpenOrCreate)) { int number = 123456789; byte[] bytes = BitConverter.GetBytes(number); fileStream.WriteBe(bytes); } } }In this example, we open an existing file or create it if it doesn't exist using the FileMode.OpenOrCreate option. We then convert an integer value into a byte array using the BitConverter class and write it to the file using the WriteBe method. The FileStream and System.IO namespace are a part of the .NET Framework Class Library package.