Hi Guys!!
This sample code will do help you for the above topic named here, and solved like this already.. any issues you make me a query i give further information..
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Runtime.Serialization;
namespace fileSerialization
{
public partial class Form1 : Form
{
TestStruc mp,np;
BinaryFormatter bformatter;
Stream stream;
public Form1()
{
s1 = new TestStruc();
s1.EmpId = 1;
s1.EmpName = “Raj”;
s1.CompCode = “Code1″;
s2 = new TestStruc();
s2.EmpId = 2;
s2.EmpName = “Peter”;
s2.CompCode = “Code2″;
}
[Serializable()] //Set this attribute to all the classes that want to serialize
public struct TestStruc : ISerializable
{
public int EmpId;
public string EmpName;
public string CompCode;
//Serialization function.
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
//You can use any custom name for your name-value pair. But make sure you
// read the values with the same name. For ex:- If you write EmpId as “EmployeeId”
// then you should read the same with “EmployeeId”
info.AddValue(”EmployeeId”, EmpId);
info.AddValue(”EmployeeName”, EmpName);
info.AddValue(”CompCode”, CompCode);
}
//Deserialization constructor.
public TestStruc(SerializationInfo info, StreamingContext ctxt)
{
//Get the values from info and assign them to the appropriate properties
EmpId = (int)info.GetValue(”EmployeeId”, typeof(int));
EmpName = (String)info.GetValue(”EmployeeName”, typeof(string));
CompCode = (String)info.GetValue(”CompCode”, typeof(string));
}
// Serialise the object Stream.
private void button1_Click(object sender, EventArgs e)
{
stream = File.Open(”EmployeeInfo.txt”, FileMode.Create);
bformatter = new BinaryFormatter();
bformatter.Serialize(stream, s1);
bformatter.Serialize(stream, s2);
stream.Close();
}
private void button2_Click(object sender, EventArgs e)
{
//Open the file written above and read values from it.
stream = File.Open(”EmployeeInfo.txt”, FileMode.Open);
//StreamReader stream1 = new StreamReader(stream);
bformatter = new BinaryFormatter();
Console.WriteLine(”Reading Employee Information”);
while (stream.Position < stream.Length)
{
s1 = (TestStruc)bformatter.Deserialize(stream);
MessageBox.Show(”Employee Id: {0}” + mp.EmpId.ToString());
MessageBox.Show(”Employee Name: {0}” + s1.EmpName);
MessageBox.Show(”CompCode” + s1.CompCode);
}
}
}
}
সর্বশেষ এডিট : ২২ শে সেপ্টেম্বর, ২০০৯ রাত ১:০৩

অনুগ্রহ করে অপেক্ষা করুন। ছবি আটো ইন্সার্ট হবে।


