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
here’s MainForm_Load(sender, e) method
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
No comments:
Post a Comment