里面用到了TreeView,数据有1500多条,加载要10秒钟左右
柳永法(yongfa365)'Blog的特点是:有问题,就要解决,尤其是像这种几十人、上百人使用的系统。
后来使用登录时加载数据,登录后再使用就Clone()的解决方案,感觉不错。
不过,每次调试程序时得等半天,实在不爽。
前些天看到“序列化”这个概念,大意就是把对象保存为一个文件,下次再使用时,反序列化一下就OK了,第二天一大早到公司立马做了个Demo,哇,不错,非常之不错,使用此方法后,根本没有延迟的现象。今天终于应用到这个项目中了,同志们再也不用等那10秒了。
特记录于此,供后人瞻仰
- using System;
- using System.IO;
- using System.Windows.Forms;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Data;
- //from:http://www.ecjtu.org/forum/read.php?tid-12933.html
- //反序列化:SerializeTree.TreeViewDataAccess.LoadTreeViewData(treeView1,"C:\\treeview.txt");
- //序列化: SerializeTree.TreeViewDataAccess.SaveTreeViewData(treeView1,"C:\\treeview.txt");
- namespace SerializeTree
- {
- /// <summary>
- /// TreeView串行化类
- /// </summary>
- public class TreeViewDataAccess
- {
- public TreeViewDataAccess() { }
- /// <summary>
- /// TreeViewData
- /// </summary>
- [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>
- /// 通过TreeViewData弹出TreeView
- /// </summary>
- /// <param name="treeview"></param>
- public void PopulateTree(TreeView treeview)
- {
- if (this.Nodes == null || this.Nodes.Length == 0)
- {
- return;
- }
- treeview.BeginUpdate();
- for (int i = 0; i <= this.Nodes.Length - 1; i++)
- {
- treeview.Nodes.Add(this.Nodes[i].ToTreeNode());
- }
- treeview.EndUpdate();
- }
- }
- /// <summary>
- /// TreeNodeData
- /// </summary>
- [Serializable()]
- public struct TreeNodeData
- {
- public string Text;
- public int ImageIndex;
- public int SelectedImageIndex;
- public bool Checked;
- public bool Expanded;
- public object Tag;
- public TreeNodeData[] Nodes;
- /// <summary>
- /// TreeNode构造函数
- /// </summary>
- /// <param name="node"></param>
- public TreeNodeData(TreeNode node)
- {
- this.Text = node.Text;
- this.ImageIndex = node.ImageIndex;
- this.SelectedImageIndex = node.SelectedImageIndex;
- this.Checked = node.Checked;
- this.Expanded = node.IsExpanded;
- this.Nodes = new TreeNodeData[node.Nodes.Count];
- if ((!(node.Tag == null)) && node.Tag.GetType().IsSerializable)
- {
- this.Tag = node.Tag;
- }
- else
- {
- this.Tag = null;
- }
- if (node.Nodes.Count == 0)
- {
- return;
- }
- for (int i = 0; i <= node.Nodes.Count - 1; i++)
- {
- Nodes[i] = new TreeNodeData(node.Nodes[i]);
- }
- }
- /// <summary>
- /// TreeNodeData返回TreeNode
- /// </summary>
- /// <returns></returns>
- public TreeNode ToTreeNode()
- {
- TreeNode ToTreeNode = new TreeNode(this.Text, this.ImageIndex, this.SelectedImageIndex);
- ToTreeNode.Checked = this.Checked;
- ToTreeNode.Tag = this.Tag;
- if (this.Expanded)
- {
- ToTreeNode.Expand();
- }
- if (this.Nodes == null && this.Nodes.Length == 0)
- {
- return null;
- }
- if (ToTreeNode != null && this.Nodes.Length == 0)
- {
- return ToTreeNode;
- }
- for (int i = 0; i <= this.Nodes.Length - 1; i++)
- {
- ToTreeNode.Nodes.Add(this.Nodes[i].ToTreeNode());
- }
- return ToTreeNode;
- }
- }
- /// <summary>
- /// 加载TreeView
- /// </summary>
- /// <param name="treeView"></param>
- /// <param name="path"></param>
- public static void LoadTreeViewData(TreeView treeView, string path)
- {
- BinaryFormatter ser = new BinaryFormatter();
- Stream file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
- TreeViewData treeData = ((TreeViewData)(ser.Deserialize(file)));
- treeData.PopulateTree(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();
- }
- }
- //柳永法加的,序列化,及反序列化DataTable
- class SerializeDataTable
- {
- 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();
- }
- }