- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Runtime.Serialization;
- using System.IO;
- using System.Runtime.Serialization.Formatters.Binary;
- namespace SerializationWithWINForm
- {
- [Serializable]
- public class SerializeProp
- {
- private int _id;
- private int _number;
- private string _empname = string.Empty;
- private string _address = string.Empty;
- public SerializeProp()
- {
- }
- public int ID
- {
- get
- {
- return _id;
- }
- set
- {
- _id = value;
- }
- }
- public int Number
- {
- get
- {
- return _number;
- }
- set
- {
- _number = value;
- }
- }
- public string Address
- {
- get
- {
- return _address;
- }
- set
- {
- _address = value;
- }
- }
- public string EmpName
- {
- get
- {
- return _empname;
- }
- set
- {
- _empname = value;
- }
- }
- }
- }
- using System;
- 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.IO;
- using System.Runtime.Serialization;
- using System.Runtime.Serialization.Formatters;
- using System.Runtime.Serialization.Formatters.Binary;
- namespace SerializationWithWINForm
- {
- public partial class SerializeForm :Form
- {
- public SerializeForm()
- {
- InitializeComponent();
- }
- SerializeProp objISerializeProp = new SerializeProp();
- IFormatter objBinaryFormatter = new BinaryFormatter();
- private string _filePath = "C:\\WinFormSerializeLog.txt";
- private void button1_Click(object sender, EventArgs e)
- {
- objISerializeProp.ID = Convert.ToInt32(txtID.Text);
- objISerializeProp.EmpName = txtName.Text;
- objISerializeProp.Number = Convert.ToInt32(txtNumber.Text);
- objISerializeProp.Address = txtAddress.Text;
- }
- private void button2_Click(object sender, EventArgs e)
- {
- Stream objStream = new FileStream(_filePath,
- FileMode.Create,
- FileAccess.ReadWrite,
- FileShare.None);
- objISerializeProp.ID = objISerializeProp.ID;
- objISerializeProp.EmpName = objISerializeProp.EmpName;
- objISerializeProp.Number = objISerializeProp.Number;
- objISerializeProp.Address = objISerializeProp.Address;
- objBinaryFormatter.Serialize(objStream, objISerializeProp);
- objStream.Close();
- txtID.Text = "";
- txtName.Text = "";
- txtNumber.Text = "";
- txtAddress.Text = "";
- }
- private void SerializeForm_Load(object sender, EventArgs e)
- {
- Stream objStreamDeSerialize = new FileStream(_filePath,
- FileMode.Open,
- FileAccess.Read,
- FileShare.Read);
- objISerializeProp = (SerializeProp)objBinaryFormatter.Deserialize(objStreamDeSerialize);
- txtID.Text = objISerializeProp.ID.ToString();
- txtName.Text = objISerializeProp.EmpName;
- txtNumber.Text = objISerializeProp.Number.ToString();
- txtAddress.Text = objISerializeProp.Address;
- objStreamDeSerialize.Close();
- }
- private void button3_Click(object sender, EventArgs e)
- {
- txtID.Text = "";
- txtName.Text = "";
- txtNumber.Text = "";
- txtAddress.Text = "";
- }
- }
- }
- namespace SerializationWithWINForm
- {
- partial class SerializeForm
- {
- /// <summary>
- /// Required designer variable.
- /// </summary>
- private System.ComponentModel.IContainer components = null;
- /// <summary>
- /// Clean up any resources being used.
- /// </summary>
- /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
- protected override void Dispose(bool disposing)
- {
- if (disposing && (components != null))
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
- #region Windows Form Designer generated code
- /// <summary>
- /// Required method for Designer support - do not modify
- /// the contents of this method with the code editor.
- /// </summary>
- private void InitializeComponent()
- {
- this.button1 = new System.Windows.Forms.Button();
- this.button2 = new System.Windows.Forms.Button();
- this.txtID = new System.Windows.Forms.TextBox();
- this.txtName = new System.Windows.Forms.TextBox();
- this.txtNumber = new System.Windows.Forms.TextBox();
- this.txtAddress = new System.Windows.Forms.TextBox();
- this.label1 = new System.Windows.Forms.Label();
- this.label2 = new System.Windows.Forms.Label();
- this.label3 = new System.Windows.Forms.Label();
- this.label4 = new System.Windows.Forms.Label();
- this.button3 = new System.Windows.Forms.Button();
- this.SuspendLayout();
- //
- // button1
- //
- this.button1.Location = new System.Drawing.Point(12, 204);
- this.button1.Name = "button1";
- this.button1.Size = new System.Drawing.Size(75, 23);
- this.button1.TabIndex = 0;
- this.button1.Text = "ok";
- this.button1.UseVisualStyleBackColor = true;
- this.button1.Click += new System.EventHandler(this.button1_Click);
- //
- // button2
- //
- this.button2.Location = new System.Drawing.Point(93, 204);
- this.button2.Name = "button2";
- this.button2.Size = new System.Drawing.Size(75, 23);
- this.button2.TabIndex = 1;
- this.button2.Text = "Serialize";
- this.button2.UseVisualStyleBackColor = true;
- this.button2.Click += new System.EventHandler(this.button2_Click);
- //
- // txtID
- //
- this.txtID.Location = new System.Drawing.Point(181, 13);
- this.txtID.Name = "txtID";
- this.txtID.Size = new System.Drawing.Size(100, 20);
- this.txtID.TabIndex = 2;
- //
- // txtName
- //
- this.txtName.Location = new System.Drawing.Point(181, 54);
- this.txtName.Name = "txtName";
- this.txtName.Size = new System.Drawing.Size(100, 20);
- this.txtName.TabIndex = 3;
- //
- // txtNumber
- //
- this.txtNumber.Location = new System.Drawing.Point(181, 102);
- this.txtNumber.Name = "txtNumber";
- this.txtNumber.Size = new System.Drawing.Size(100, 20);
- this.txtNumber.TabIndex = 4;
- //
- // txtAddress
- //
- this.txtAddress.Location = new System.Drawing.Point(181, 152);
- this.txtAddress.Name = "txtAddress";
- this.txtAddress.Size = new System.Drawing.Size(100, 20);
- this.txtAddress.TabIndex = 5;
- //
- // label1
- //
- this.label1.AutoSize = true;
- this.label1.Location = new System.Drawing.Point(36, 19);
- this.label1.Name = "label1";
- this.label1.Size = new System.Drawing.Size(18, 13);
- this.label1.TabIndex = 6;
- this.label1.Text = "ID";
- //
- // label2
- //
- this.label2.AutoSize = true;
- this.label2.Location = new System.Drawing.Point(39, 60);
- this.label2.Name = "label2";
- this.label2.Size = new System.Drawing.Size(35, 13);
- this.label2.TabIndex = 7;
- this.label2.Text = "Name";
- //
- // label3
- //
- this.label3.AutoSize = true;
- this.label3.Location = new System.Drawing.Point(39, 108);
- this.label3.Name = "label3";
- this.label3.Size = new System.Drawing.Size(44, 13);
- this.label3.TabIndex = 8;
- this.label3.Text = "Number";
- //
- // label4
- //
- this.label4.AutoSize = true;
- this.label4.Location = new System.Drawing.Point(42, 158);
- this.label4.Name = "label4";
- this.label4.Size = new System.Drawing.Size(45, 13);
- this.label4.TabIndex = 9;
- this.label4.Text = "Address";
- //
- // button3
- //
- this.button3.Location = new System.Drawing.Point(174, 204);
- this.button3.Name = "button3";
- this.button3.Size = new System.Drawing.Size(75, 23);
- this.button3.TabIndex = 10;
- this.button3.Text = "Clear";
- this.button3.UseVisualStyleBackColor = true;
- this.button3.Click += new System.EventHandler(this.button3_Click);
- //
- // SerializeForm
- //
- this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- this.ClientSize = new System.Drawing.Size(292, 266);
- this.Controls.Add(this.button3);
- this.Controls.Add(this.label4);
- this.Controls.Add(this.label3);
- this.Controls.Add(this.label2);
- this.Controls.Add(this.label1);
- this.Controls.Add(this.txtAddress);
- this.Controls.Add(this.txtNumber);
- this.Controls.Add(this.txtName);
- this.Controls.Add(this.txtID);
- this.Controls.Add(this.button2);
- this.Controls.Add(this.button1);
- this.Name = "SerializeForm";
- this.Text = "Form1";
- this.Load += new System.EventHandler(this.SerializeForm_Load);
- this.ResumeLayout(false);
- this.PerformLayout();
- }
- #endregion
- private System.Windows.Forms.Button button1;
- private System.Windows.Forms.Button button2;
- private System.Windows.Forms.TextBox txtID;
- private System.Windows.Forms.TextBox txtName;
- private System.Windows.Forms.TextBox txtNumber;
- private System.Windows.Forms.TextBox txtAddress;
- private System.Windows.Forms.Label label1;
- private System.Windows.Forms.Label label2;
- private System.Windows.Forms.Label label3;
- private System.Windows.Forms.Label label4;
- private System.Windows.Forms.Button button3;
- }
- }


Join the conversation! Your thoughts help the community grow.