Getsuyodev, Programming is mainly used when constructing an application. Programming requires knowledge of application domains, algorithms, and programming language expertise. Programming can be developed in different logic based on developer knowledge.

How to use C-Sharp BinaryWriter Class



C# BinaryWriter Object works at a lower level of Streams. C# BinaryWriter Class is using for writing primitive types as binary values in a specific encoding stream. C# BinaryWriter Object works with Stream Objects that provide access to the underlying bytes. For creating a BinaryWriter Object, you have first to create a FileStream Object and then pass BinaryWriter to the constructor method.

FileStream writeStream ;
writeStream = new FileStream("c:\\testBinary.dat", FileMode.Create);
BinaryWriter writeBinay = new BinaryWriter(writeStream);

The main advantages of Binary information are that it is not easily human-readable and stores files as Binary format is the best practice of space utilization.

using System;
using System.Windows.Forms;
using System.IO;
using System.Text;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FileStream writeStream ;
            try
            {
                writeStream = new FileStream("c:\\getsuyodev.dat", FileMode.Create);
                BinaryWriter writeBinay = new BinaryWriter(writeStream);
                writeBinay.Write("getsuyodev.com binary writer test");
                writeBinay.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show (ex.ToString());
            }
        }
    }
}

BinaryReader you can use in the same way to read as binary.


Comments are closed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More