Byte Array to a text file

C# is a strongly typed language. That means that every object you create or use in a C# program must have a specific type. Byte is an immutable value type that represents unsigned integers with values that range from 0 to 255 .

Here we open a FileStream class instance and using the method Write() . Write() method writes a block of bytes to the current stream using data read from buffer.

oFileStream.Write(byteData, 0, byteData.Length);

The following C# program shows how to write the content of a Byte Array into a Text file.

using System; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string str = ""; System.Text.UTF8Encoding encod = new System.Text.UTF8Encoding(); byte[] byteData = encod.GetBytes(str); System.IO.FileStream oFileStream = null; oFileStream = new System.IO.FileStream("c:\\bytes.txt", System.IO.FileMode.Create); oFileStream.Write(byteData, 0, byteData.Length); oFileStream.Close(); } } }