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. }
  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. private void button1_Click(object sender, EventArgs e)
  25. {
  26. objISerializeProp.ID = Convert.ToInt32(txtID.Text);
  27. objISerializeProp.EmpName = txtName.Text;
  28. objISerializeProp.Number = Convert.ToInt32(txtNumber.Text);
  29. objISerializeProp.Address = txtAddress.Text;
  30. }
  31. private void button2_Click(object sender, EventArgs e)
  32. {
  33. Stream objStream = new FileStream(_filePath,
  34. FileMode.Create,
  35. FileAccess.ReadWrite,
  36. FileShare.None);
  37. objISerializeProp.ID = objISerializeProp.ID;
  38. objISerializeProp.EmpName = objISerializeProp.EmpName;
  39. objISerializeProp.Number = objISerializeProp.Number;
  40. objISerializeProp.Address = objISerializeProp.Address;
  41. objBinaryFormatter.Serialize(objStream, objISerializeProp);
  42. objStream.Close();
  43. txtID.Text = "";
  44. txtName.Text = "";
  45. txtNumber.Text = "";
  46. txtAddress.Text = "";
  47. }
  48. private void SerializeForm_Load(object sender, EventArgs e)
  49. {
  50. Stream objStreamDeSerialize = new FileStream(_filePath,
  51. FileMode.Open,
  52. FileAccess.Read,
  53. FileShare.Read);
  54. objISerializeProp = (SerializeProp)objBinaryFormatter.Deserialize(objStreamDeSerialize);
  55. txtID.Text = objISerializeProp.ID.ToString();
  56. txtName.Text = objISerializeProp.EmpName;
  57. txtNumber.Text = objISerializeProp.Number.ToString();
  58. txtAddress.Text = objISerializeProp.Address;
  59. objStreamDeSerialize.Close();
  60. }
  61. private void button3_Click(object sender, EventArgs e)
  62. {
  63. txtID.Text = "";
  64. txtName.Text = "";
  65. txtNumber.Text = "";
  66. txtAddress.Text = "";
  67. }
  68. }
  69. }
  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. }