Using Binary file for storing data


 

Hi friends,

Hope you al are having great time and coding experience.

Today we’ll learn about using Binary file for storing data. From, some last days i was suffering with a problem. I used to subscribe for training and webinars but i rarely manage to attend them the reason is just that i used to forget about them so i decided to make a meeting reminder. i didn’t wanted to use any DATABASE for this hence i decided to use Binary file. here it’ is.

 

Basically what here i’m going to discuss about is how to use binary file for storing data. here i used it for storing meeting information.

I have used the concept of serialization for writing an object into a binary file using binary formatter.

Serialization is the process by which a .NET object can be converted into a
stream, which can easily be transferred across a network or written to disk.
This stream can be converted into a copy of the original object through a
process called deserialization.

i made a class which define meetings and its detail and serialized it using [Serializable()]

here’s code

 

[Serializable()]
public class MeetingData
{
public string date;
public string time;
public string name;
public string Description;
public string Meeting_Id;
}

This is class which object we are going to write into a binary file.

Following is the step we are going to follow.

  • Taking meeting infos
  • Adding that to MeetingData List
  • writing list to binary file

Taking info is simple, we can take it from UI on button click, here’s function for that

void addMeeting(MeetingData Md)
{
Md.date = CAl.SelectionRange.Start.Day.ToString() + “/” + CAl.SelectionRange.Start.Month.ToString();
Md.time = txtTime.Text;
Md.name = txtName.Text;
Md.Description = txtDes.Text;
Md.Meeting_Id = (Last_Meeting_Count + 1).ToString();
}

CAl is callender control.

I have created a list of MeetingData class that would contain all meetings

List<MeetingData> M_list = new List<MeetingData>();

Adding Meetings to list, here’s function for that

void addtolist(MeetingData md)
{
M_list.Add(md);
}

Writing to file usase Binary Formatter, here’s function for that

void WriteFile()
{
FileStream fs = File.OpenWrite(“meeting.bin”);
try
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, M_list);
fs.Close();
}
catch (Exception x)
{
fs.Close();
}
}

Now on button click to add information to file here’s click event

private void button1_Click(object sender, EventArgs e)
{
MeetingData md = new MeetingData();
addMeeting(md);
addtolist(md);
WriteFile();
MainForm_Load(sender, e);
}

Now while loading application we need to get data back from Binary file. Here we’ll do Deserialization

private void MainForm_Load(object sender, EventArgs e)
{
txtName.Focus();
listView1.Items.Clear();
FileStream fs = File.OpenRead(“meeting.bin”);
try
{
BinaryFormatter bf = new BinaryFormatter();
List<MeetingData> Md = bf.Deserialize(fs) as List<MeetingData>;
foreach (MeetingData m in Md)
{
listView1.Items.Add(new ListViewItem(new string[] {m.Meeting_Id, m.time + ” ” + m.date + @”\” + CAl.SelectionRange.Start.Month, m.name, m.Description }));
}
M_list = Md;
fs.Close();
}
catch (Exception x)
{
fs.Close();
}
}

Namespaces used are

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;

You can download complete setup and source code from here

http://meetingreminder.codeplex.com/

Advertisements

12 thoughts on “Using Binary file for storing data

    • Captcha code is used for staying away from span. it is used in any form where you post something. they provide you some sort of code and asks you to enter it in a textbox and this way they differentiate between user and spam.
      we are about to make post on catpcha. pleae Keep visiting this blog.

  1. Man, talk about a fantastic post! I?ve stumbled across your blog a few times within the past, but I usually forgot to bookmark it. But not again! Thanks for posting the way you do, I genuinely appreciate seeing someone who actually has a viewpoint and isn?t really just bringing back up crap like nearly all other writers today. Keep it up!

  2. Hello!, Very interest angle, we were talking about the same thing at work and found your site very stimulating. So felt compelled to com?ment a little thank you for all your effort. Please keep up the great work your doing!

  3. This is second incident that I am scanning anything about modifying websites with the system. It seems that you are an super expert blogger. Your post is an excellent example of why I continue coming back to study your good quality content that is forever updated.

  4. I keep listening on the news converse about obtaining free on the net grant applications so I’ve been seeking about for the most effective internet site to obtain one. Thank you for the help!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s