Serialization -DeSerialization of Windows Form in C#

 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Runtime.Serialization;  
  6. using System.IO;  
  7. using System.Runtime.Serialization.Formatters.Binary;  
  8. namespace SerializationWithWINForm  
  9. {  
  10.  [Serializable]  
  11.  public class SerializeProp  
  12.  {  
  13.      private int _id;  
  14.      private int _number;  
  15.      private string _empname = string.Empty;  
  16.      private string _address = string.Empty;  
  17.      public SerializeProp()  
  18.      {  
  19.      }  
  20.      public int ID  
  21.      {  
  22.          get  
  23.          {  
  24.              return _id;  
  25.          }  
  26.          set  
  27.          {  
  28.              _id = value;  
  29.          }  
  30.      }  
  31.      public int Number  
  32.      {  
  33.          get  
  34.          {  
  35.              return _number;  
  36.          }  
  37.          set  
  38.          {  
  39.              _number = value;  
  40.          }  
  41.      }          
  42.      public string Address  
  43.      {  
  44.          get  
  45.          {  
  46.              return _address;  
  47.          }  
  48.          set  
  49.          {  
  50.              _address = value;  
  51.          }  
  52.      }  
  53.      public string EmpName  
  54.      {  
  55.          get  
  56.          {  
  57.              return _empname;  
  58.          }  
  59.          set  
  60.          {  
  61.              _empname = value;  
  62.          }  
  63.      }  
  64.        
  65.  }  
  66. }  
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.IO;  
  10. using System.Runtime.Serialization;  
  11. using System.Runtime.Serialization.Formatters;  
  12. using System.Runtime.Serialization.Formatters.Binary;  
  13. namespace SerializationWithWINForm  
  14. {  
  15.       public partial class SerializeForm :Form  
  16.       {  
  17.             public SerializeForm()  
  18.             {  
  19.                   InitializeComponent();  
  20.             }  
  21.             SerializeProp objISerializeProp = new SerializeProp();  
  22.             IFormatter objBinaryFormatter = new BinaryFormatter();  
  23.             private string _filePath = "C:\\WinFormSerializeLog.txt";  
  24.               
  25.             private void button1_Click(object sender, EventArgs e)  
  26.             {  
  27.                   objISerializeProp.ID = Convert.ToInt32(txtID.Text);  
  28.                   objISerializeProp.EmpName = txtName.Text;  
  29.                   objISerializeProp.Number = Convert.ToInt32(txtNumber.Text);  
  30.                   objISerializeProp.Address = txtAddress.Text;  
  31.             }  
  32.   
  33.             private void button2_Click(object sender, EventArgs e)  
  34.             {  
  35.                   Stream objStream = new FileStream(_filePath,  
  36.                   FileMode.Create,  
  37.                   FileAccess.ReadWrite,  
  38.                   FileShare.None);  
  39.                   objISerializeProp.ID = objISerializeProp.ID;  
  40.                   objISerializeProp.EmpName = objISerializeProp.EmpName;  
  41.                   objISerializeProp.Number = objISerializeProp.Number;  
  42.                   objISerializeProp.Address = objISerializeProp.Address;  
  43.                   objBinaryFormatter.Serialize(objStream, objISerializeProp);  
  44.                   objStream.Close();  
  45.                   txtID.Text = "";  
  46.                   txtName.Text = "";  
  47.                   txtNumber.Text = "";  
  48.                   txtAddress.Text = "";  
  49.             }  
  50.   
  51.             private void SerializeForm_Load(object sender, EventArgs e)  
  52.             {  
  53.                   Stream objStreamDeSerialize = new FileStream(_filePath,  
  54.                   FileMode.Open,  
  55.                   FileAccess.Read,  
  56.                   FileShare.Read);  
  57.                   objISerializeProp = (SerializeProp)objBinaryFormatter.Deserialize(objStreamDeSerialize);  
  58.                   txtID.Text = objISerializeProp.ID.ToString();  
  59.                   txtName.Text = objISerializeProp.EmpName;  
  60.                   txtNumber.Text = objISerializeProp.Number.ToString();  
  61.                   txtAddress.Text = objISerializeProp.Address;  
  62.                   objStreamDeSerialize.Close();  
  63.             }  
  64.               
  65.             private void button3_Click(object sender, EventArgs e)  
  66.             {  
  67.                   txtID.Text = "";  
  68.                   txtName.Text = "";  
  69.                   txtNumber.Text = "";  
  70.                   txtAddress.Text = "";  
  71.             }  
  72.       }  
  73. }  
 
  1. namespace SerializationWithWINForm  
  2. {  
  3.     partial class SerializeForm  
  4.     {  
  5.         /// <summary>  
  6.         /// Required designer variable.  
  7.         /// </summary>  
  8.         private System.ComponentModel.IContainer components = null;  
  9.         /// <summary>  
  10.         /// Clean up any resources being used.  
  11.         /// </summary>  
  12.         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>  
  13.         protected override void Dispose(bool disposing)  
  14.         {  
  15.             if (disposing && (components != null))  
  16.             {  
  17.                 components.Dispose();  
  18.             }  
  19.             base.Dispose(disposing);  
  20.         }  
  21.         #region Windows Form Designer generated code  
  22.         /// <summary>  
  23.         /// Required method for Designer support - do not modify  
  24.         /// the contents of this method with the code editor.  
  25.         /// </summary>  
  26.         private void InitializeComponent()  
  27.         {  
  28.             this.button1 = new System.Windows.Forms.Button();  
  29.             this.button2 = new System.Windows.Forms.Button();  
  30.             this.txtID = new System.Windows.Forms.TextBox();  
  31.             this.txtName = new System.Windows.Forms.TextBox();  
  32.             this.txtNumber = new System.Windows.Forms.TextBox();  
  33.             this.txtAddress = new System.Windows.Forms.TextBox();  
  34.             this.label1 = new System.Windows.Forms.Label();  
  35.             this.label2 = new System.Windows.Forms.Label();  
  36.             this.label3 = new System.Windows.Forms.Label();  
  37.             this.label4 = new System.Windows.Forms.Label();  
  38.             this.button3 = new System.Windows.Forms.Button();  
  39.             this.SuspendLayout();  
  40.             //   
  41.             // button1  
  42.             //   
  43.             this.button1.Location = new System.Drawing.Point(12, 204);  
  44.             this.button1.Name = "button1";  
  45.             this.button1.Size = new System.Drawing.Size(75, 23);  
  46.             this.button1.TabIndex = 0;  
  47.             this.button1.Text = "ok";  
  48.             this.button1.UseVisualStyleBackColor = true;  
  49.             this.button1.Click += new System.EventHandler(this.button1_Click);  
  50.             //   
  51.             // button2  
  52.             //   
  53.             this.button2.Location = new System.Drawing.Point(93, 204);  
  54.             this.button2.Name = "button2";  
  55.             this.button2.Size = new System.Drawing.Size(75, 23);  
  56.             this.button2.TabIndex = 1;  
  57.             this.button2.Text = "Serialize";  
  58.             this.button2.UseVisualStyleBackColor = true;  
  59.             this.button2.Click += new System.EventHandler(this.button2_Click);  
  60.             //   
  61.             // txtID  
  62.             //   
  63.             this.txtID.Location = new System.Drawing.Point(181, 13);  
  64.             this.txtID.Name = "txtID";  
  65.             this.txtID.Size = new System.Drawing.Size(100, 20);  
  66.             this.txtID.TabIndex = 2;  
  67.             //   
  68.             // txtName  
  69.             //   
  70.             this.txtName.Location = new System.Drawing.Point(181, 54);  
  71.             this.txtName.Name = "txtName";  
  72.             this.txtName.Size = new System.Drawing.Size(100, 20);  
  73.             this.txtName.TabIndex = 3;  
  74.             //   
  75.             // txtNumber  
  76.             //   
  77.             this.txtNumber.Location = new System.Drawing.Point(181, 102);  
  78.             this.txtNumber.Name = "txtNumber";  
  79.             this.txtNumber.Size = new System.Drawing.Size(100, 20);  
  80.             this.txtNumber.TabIndex = 4;  
  81.             //   
  82.             // txtAddress  
  83.             //   
  84.             this.txtAddress.Location = new System.Drawing.Point(181, 152);  
  85.             this.txtAddress.Name = "txtAddress";  
  86.             this.txtAddress.Size = new System.Drawing.Size(100, 20);  
  87.             this.txtAddress.TabIndex = 5;  
  88.             //   
  89.             // label1  
  90.             //   
  91.             this.label1.AutoSize = true;  
  92.             this.label1.Location = new System.Drawing.Point(36, 19);  
  93.             this.label1.Name = "label1";  
  94.             this.label1.Size = new System.Drawing.Size(18, 13);  
  95.             this.label1.TabIndex = 6;  
  96.             this.label1.Text = "ID";  
  97.             //   
  98.             // label2  
  99.             //   
  100.             this.label2.AutoSize = true;  
  101.             this.label2.Location = new System.Drawing.Point(39, 60);  
  102.             this.label2.Name = "label2";  
  103.             this.label2.Size = new System.Drawing.Size(35, 13);  
  104.             this.label2.TabIndex = 7;  
  105.             this.label2.Text = "Name";  
  106.             //   
  107.             // label3  
  108.             //   
  109.             this.label3.AutoSize = true;  
  110.             this.label3.Location = new System.Drawing.Point(39, 108);  
  111.             this.label3.Name = "label3";  
  112.             this.label3.Size = new System.Drawing.Size(44, 13);  
  113.             this.label3.TabIndex = 8;  
  114.             this.label3.Text = "Number";  
  115.             //   
  116.             // label4  
  117.             //   
  118.             this.label4.AutoSize = true;  
  119.             this.label4.Location = new System.Drawing.Point(42, 158);  
  120.             this.label4.Name = "label4";  
  121.             this.label4.Size = new System.Drawing.Size(45, 13);  
  122.             this.label4.TabIndex = 9;  
  123.             this.label4.Text = "Address";  
  124.             //   
  125.             // button3  
  126.             //   
  127.             this.button3.Location = new System.Drawing.Point(174, 204);  
  128.             this.button3.Name = "button3";  
  129.             this.button3.Size = new System.Drawing.Size(75, 23);  
  130.             this.button3.TabIndex = 10;  
  131.             this.button3.Text = "Clear";  
  132.             this.button3.UseVisualStyleBackColor = true;  
  133.             this.button3.Click += new System.EventHandler(this.button3_Click);  
  134.             //   
  135.             // SerializeForm  
  136.             //   
  137.             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);  
  138.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;  
  139.             this.ClientSize = new System.Drawing.Size(292, 266);  
  140.             this.Controls.Add(this.button3);  
  141.             this.Controls.Add(this.label4);  
  142.             this.Controls.Add(this.label3);  
  143.             this.Controls.Add(this.label2);  
  144.             this.Controls.Add(this.label1);  
  145.             this.Controls.Add(this.txtAddress);  
  146.             this.Controls.Add(this.txtNumber);  
  147.             this.Controls.Add(this.txtName);  
  148.             this.Controls.Add(this.txtID);  
  149.             this.Controls.Add(this.button2);  
  150.             this.Controls.Add(this.button1);  
  151.             this.Name = "SerializeForm";  
  152.             this.Text = "Form1";  
  153.             this.Load += new System.EventHandler(this.SerializeForm_Load);  
  154.             this.ResumeLayout(false);  
  155.             this.PerformLayout();  
  156.         }  
  157.         #endregion  
  158.         private System.Windows.Forms.Button button1;  
  159.         private System.Windows.Forms.Button button2;  
  160.         private System.Windows.Forms.TextBox txtID;  
  161.         private System.Windows.Forms.TextBox txtName;  
  162.         private System.Windows.Forms.TextBox txtNumber;  
  163.         private System.Windows.Forms.TextBox txtAddress;  
  164.         private System.Windows.Forms.Label label1;  
  165.         private System.Windows.Forms.Label label2;  
  166.         private System.Windows.Forms.Label label3;  
  167.         private System.Windows.Forms.Label label4;  
  168.         private System.Windows.Forms.Button button3;  
  169.     }  
  170. }