序列化哈希表到文件
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Collections;
- namespace HashtableTest
- {
- public partial class Form1 : Form
- {
- private string strFile = "c:\student.dat";
- public Form1()
- {
- InitializeComponent();
- InitListviewData();
- }
- //从文件读取数据到列表
- private void InitListviewData()
- {
- Hashtable ht = BinarySerialize.DeSerialize(strFile);//反序列化文件
- if (ht != null)
- {
- IDictionaryEnumerator dic = ht.GetEnumerator();
- while (dic.MoveNext())
- {
- Student s = (Student)dic.Value;
- this.listView1.Items.AddRange(new System.Windows.Forms.ListViewItem[] {
- new ListViewItem(new string[]{s.Name,s.Number,s.Classes,s.Love})});
- }
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- Hashtable ht = new Hashtable();
- string number = this.textBox2.Text.Trim();
- if (number != "" && number != null)
- {
- ht = GetListviewData(); //获取列表数据
- if (!ht.Contains(number)) //把新添加的学生信息添加到列表并写入文件
- {
- Student s = new Student(this.textBox1.Text, this.textBox2.Text, this.textBox3.Text, this.textBox4.Text);
- ht.Add(number, s);
- this.listView1.Items.AddRange(new System.Windows.Forms.ListViewItem[] {
- new ListViewItem(new string[]{s.Name,s.Number,s.Classes,s.Love})});
- BinarySerialize.Serialize(strFile, ht);
- }
- }
- }
- //获取列表数据,并把列表数据预装到哈希表
- private Hashtable GetListviewData()
- {
- Hashtable ht = new Hashtable();
- ListView.ListViewItemCollection lvc = this.listView1.Items;
- if (lvc.Count > 0)
- {
- for (int i = 0; i < lvc.Count; i++)
- {
- Student st=new Student(lvc[i].SubItems[0].Text, lvc[i].SubItems[1].Text, lvc[i].SubItems[2].Text, lvc[i].SubItems[3].Text);
- ht.Add(st.Number, st);
- }
- return ht;
- }
- return null;
- }
- }
- #region 学生信息类
- [Serializable]
- public class Student
- {
- private String name, number, classes, love;
- public Student(String name, string number, string classes, string love)
- {
- this.name = name;
- this.number = number;
- this.classes = classes;
- this.love = love;
- }
- public string Name
- {
- get { return name; }
- }
- public string Number
- {
- get { return number; }
- }
- public string Classes
- {
- get { return classes; }
- }
- public string Love
- {
- get { return love; }
- }
- }
- #endregion
- #region 序列化哈希表到文件
- public class BinarySerialize
- {
- //把哈希表对象序列化到文件
- public static void Serialize(String strFile, Hashtable ht)
- {
- using (FileStream fs = new FileStream(strFile, FileMode.Create))
- {
- BinaryFormatter formatter = new BinaryFormatter();
- formatter.Serialize(fs, ht);
- }
- }
- //反序列化文件成哈希表对象
- public static Hashtable DeSerialize(String strFile)
- {
- Hashtable hta = null;
- using (FileStream fs = new FileStream(strFile, FileMode.Open))
- {
- BinaryFormatter formatter = new BinaryFormatter();
- hta = (Hashtable)formatter.Deserialize(fs);
- }
- return hta;
- }
- }
- #endregion
- }
复制代码
0
|
|
|
|