2009年给公司做了个管理系统,里面用到了TreeView,数据有1500多条,加载要10秒钟左右
秉着有问题,就要解决,尤其是像这种几十人、上百人使用的系统。
后来使用登录时加载数据,登录后再使用就Clone()的解决方案,感觉不错。不过,每次调试程序时得等半天,实在不爽。
前些天看到“序列化”这个概念,大意就是把对象保存为一个文件,下次再使用时,反序列化一下就OK了,第二天一大早到公司立马做了个Demo,哇,不错,非常之不错,使用此方法后,根本没有延迟的现象。今天终于应用到这个项目中了,同志们再也不用等那10秒了。
以上是背景,将其上杂乱的代码进行整理,得到整理好的对Table、TreeView序列化的类,版本1.0。
using System; using System.IO; using System.Windows.Forms; using System.Runtime.Serialization.Formatters.Binary; using System.Data; namespace DBFSR.HRDRW { /// <summary> /// 对TreeView,DataTable 对象进行序列化的存储 /// /// Usage: /// 反序列化:SerializeTree.TreeViewDataAccess.LoadTreeViewData(treeView1,"C:\treeview.txt"); /// 序列化: SerializeTree.TreeViewDataAccess.SaveTreeViewData(treeView1,"C:\treeview.txt"); /// </summary> class TreeView_Serialize { public TreeView_Serialize() { } [Serializable()] public struct TreeViewData { public TreeNodeData[] Nodes; /// <summary> /// 递归初始化TreeView数据 /// </summary> /// <param name="treeview"></param> public TreeViewData(TreeView treeview) { Nodes = new TreeNodeData[treeview.Nodes.Count]; if (treeview.Nodes.Count == 0) { return; } for (int i = 0; i <= treeview.Nodes.Count - 1; i++) { Nodes[i] = new TreeNodeData(treeview.Nodes[i]); } } // <summary> /// 弹出TreeView /// </summary> /// <param name="tv"></param> public void PopupTree(TreeView tv) { if (this.Nodes == null || this.Nodes.Length == 0) { return; } tv.BeginUpdate(); for (int i = 0; i <= this.Nodes.Length - 1; i++) { tv.Nodes.Add(this.Nodes[i].GetTreeNodes()); } tv.EndUpdate(); } } /// <summary> /// 对TreeNode数据对象进行序列化 /// </summary> [Serializable()] public struct TreeNodeData { public string m_strText; public int m_nImageIndex; public int m_nSelectedImageIndex; public bool m_bChecked; public bool m_bExpanded; public object m_Tag; public TreeNodeData[] m_Nodes; /// <summary> /// TreeNode构造函数 /// </summary> /// <param name="tn">传入初始化内容</param> public TreeNodeData(TreeNode tn) { this.m_strText = tn.Text; this.m_nImageIndex = tn.ImageIndex; this.m_nSelectedImageIndex = tn.SelectedImageIndex; this.m_bChecked = tn.Checked; this.m_bExpanded = tn.IsExpanded; this.m_Nodes = new TreeNodeData[tn.Nodes.Count]; if ((!(tn.Tag == null)) && tn.Tag.GetType().IsSerializable) this.m_Tag = tn.Tag; else this.m_Tag = null; if (tn.Nodes.Count == 0) return; for (int i = 0; i <= tn.Nodes.Count - 1; i++) m_Nodes[i] = new TreeNodeData(tn.Nodes[i]); } /// <summary> /// 返回TreeNode数据 /// </summary> /// <returns></returns> public TreeNode GetTreeNodes() { TreeNode ToTreeNode = new TreeNode(this.m_strText, this.m_nImageIndex, this.m_nSelectedImageIndex); ToTreeNode.Checked = this.m_bChecked; ToTreeNode.Tag = this.m_Tag; if (this.m_bExpanded) { ToTreeNode.Expand(); } if (this.m_Nodes == null && this.m_Nodes.Length == 0) { return null; } if (ToTreeNode != null && this.m_Nodes.Length == 0) { return ToTreeNode; } for (int i = 0; i <= this.m_Nodes.Length - 1; i++) { ToTreeNode.Nodes.Add(this.m_Nodes[i].GetTreeNodes()); } return ToTreeNode; } } /// <summary> /// 加载TreeView /// </summary> /// <param name="treeView"></param> /// <param name="path"></param> public static void LoadTreeViewData(TreeView treeView, string path) { BinaryFormatter bfmter = new BinaryFormatter(); Stream file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read); TreeViewData treeData = ((TreeViewData)(bfmter.Deserialize(file))); treeData.PopupTree(treeView); file.Close(); } /// <summary> /// 保存TreeView到文件 /// </summary> /// <param name="treeView"></param> /// <param name="path"></param> public static void SaveTreeViewData(TreeView treeView, string path) { BinaryFormatter ser = new BinaryFormatter(); Stream file = new FileStream(path, FileMode.Create); ser.Serialize(file, new TreeViewData(treeView)); file.Close(); } } /// <summary> /// 序列化,及反序列化DataTable /// </summary> class DataTable_Serialize { public static DataTable LoadDataTable(string path) { BinaryFormatter ser = new BinaryFormatter(); Stream file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read); DataTable dt = (DataTable)ser.Deserialize(file); file.Close(); return dt; } public static void SaveDataTable(DataTable dt, string path) { BinaryFormatter ser = new BinaryFormatter(); Stream file = new FileStream(path, FileMode.Create); ser.Serialize(file, dt); file.Close(); } } }