C# Exercises

Home AgriMetSoft About Contact

Read and Write Bin File in WinForms C#

	    
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Binary_File_Read_Write
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
      comboBox1.Items.Add("String");
      comboBox1.Items.Add("Double");
      comboBox1.Items.Add("Int16");
      comboBox1.SelectedIndex = 0;
    }

    private void write_Click(object sender, EventArgs e)
    {
      using (SaveFileDialog sfd = new SaveFileDialog() { Filter = @"Binary|*.bin" })
      {
        if (sfd.ShowDialog() == DialogResult.OK)
        {
          using (System.IO.BinaryWriter binWriter = new System.IO.BinaryWriter(File.Open(sfd.FileName, FileMode.Create)))
          {
            var data = textBox1.Text.Split(',');
            if (comboBox1.SelectedIndex == 0)
            {
              binWriter.Write(Encoding.UTF8.GetBytes(textBox1.Text));
            }
            if (comboBox1.SelectedIndex == 1)
            {
              foreach (string item in data)
                binWriter.Write(Convert.ToDouble(item));
            }            
            if (comboBox1.SelectedIndex == 2)
            {
              foreach (string item in data)
                binWriter.Write(Convert.ToInt16(item));
            }
            binWriter.Close();
          }
          MessageBox.Show("The Data is Saved.");
        }
      }
       
    }

    private void read_Click(object sender, EventArgs e)
    {
      using (OpenFileDialog ofd = new OpenFileDialog() { Filter = @"Binary|*.bin" })
      {
        if (ofd.ShowDialog() == DialogResult.OK)
        {
          Stream sr = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read);
          byte[] buffer = new byte[sr.Length];
          sr.Read(buffer, 0, (int)sr.Length);
          var fileCode = BitConverter.ToUInt32(buffer, 0);
          string str = "";
          List<double> dataDouble = new List<double>();
          List<Int16> dataInt16 = new List<Int16>();
          if (comboBox1.SelectedIndex == 0)
          {
            str = Encoding.UTF8.GetString(buffer);
          }
          if (comboBox1.SelectedIndex == 1)
          {
            for (int i = 0; i < buffer.Length; i += 8)
            {
              dataDouble.Add(BitConverter.ToDouble(buffer, i));
              str += dataDouble[i / 8].ToString() + ",";
            }
            str.Remove(str.Length - 1, 1);//Remove end comma ,
          }
          if (comboBox1.SelectedIndex == 2)
          {
            for (int i = 0; i < buffer.Length; i += 2)
            {
              dataInt16.Add(BitConverter.ToInt16(buffer, i));
              str += dataInt16[i / 2].ToString() + ",";
            }
            str.Remove(str.Length - 1, 1);//Remove end comma ,
          }

          textBox1.Text = str;
          sr.Close();
          MessageBox.Show("The Data is Saved.");
        }
      }

    }
  }
}
		
	 


Download the project of Visual Studio 2013 in DropBox Download


How to Read and Write Binary Files in Winforms C#


List of Exercises