一个C#序列化TreeView的方法(一个类)

 
  1. using System;
  2. using System.IO;
  3. using System.Windows.Forms;
  4. using System.Runtime.Serialization.Formatters.Binary;
  5. namespace SerializeTree
  6. {
  7.     /// 
  8.     /// TreeView串行化类
  9.     /// 
  10.     public class TreeViewDataAccess
  11.     {
  12.         public TreeViewDataAccess(){}
  13.   
  14.         /// 
  15.         /// TreeViewData
  16.         /// 
  17.         [Serializable()]
  18.         public struct TreeViewData
  19.         {
  20.             public TreeNodeData[] Nodes;
  21.             /// 
  22.             /// 递归初始化TreeView数据
  23.             /// 
  24.             /// 
  25.             public TreeViewData(TreeView treeview)
  26.             {
  27.                 Nodes = new TreeNodeData[treeview.Nodes.Count];
  28.                 if (treeview.Nodes.Count == 0)
  29.                 {
  30.                     return;
  31.                 }
  32.                 for (int i = 0; i <= treeview.Nodes.Count - 1; i++) 
  33.                 {
  34.                     Nodes[i] = new TreeNodeData(treeview.Nodes[i]);
  35.                 }
  36.             }
  37.             /// 
  38.             /// 通过TreeViewData弹出TreeView
  39.             /// 
  40.             /// 
  41.             public void PopulateTree(TreeView treeview)
  42.             {
  43.                 if (this.Nodes == null || this.Nodes.Length == 0) 
  44.                 {
  45.                     return;
  46.                 }
  47.                 treeview.BeginUpdate();
  48.                 for (int i = 0; i <= this.Nodes.Length - 1; i++) 
  49.                 {
  50.                     treeview.Nodes.Add(this.Nodes[i].ToTreeNode());
  51.                 }
  52.                 treeview.EndUpdate();
  53.             }
  54.         }
  55.         /// 
  56.         /// TreeNodeData
  57.         /// 
  58.         [Serializable()]
  59.         public struct TreeNodeData
  60.         {
  61.             public string Text;
  62.             public int ImageIndex;
  63.             public int SelectedImageIndex;
  64.             public bool Checked;
  65.             public bool Expanded;
  66.             public object Tag;
  67.             public TreeNodeData[] Nodes;
  68.             /// 
  69.             /// TreeNode构造函数
  70.             /// 
  71.             /// 
  72.             public TreeNodeData(TreeNode node)
  73.             {
  74.                 this.Text = node.Text;
  75.                 this.ImageIndex = node.ImageIndex;
  76.                 this.SelectedImageIndex = node.SelectedImageIndex;
  77.                 this.Checked = node.Checked;
  78.                 this.Expanded = node.IsExpanded;
  79.                 this.Nodes = new TreeNodeData[node.Nodes.Count];
  80.                 if ((!(node.Tag == null)) && node.Tag.GetType().IsSerializable) 
  81.                 {
  82.                     this.Tag = node.Tag;
  83.                 }
  84.                 else
  85.                 {
  86.                     this.Tag = null;
  87.                 }
  88.                 if (node.Nodes.Count == 0) 
  89.                 {
  90.                     return;
  91.                 }
  92.                 for (int i = 0; i <= node.Nodes.Count - 1; i++) 
  93.                 {
  94.                     Nodes[i] = new TreeNodeData(node.Nodes[i]);
  95.                 }
  96.             }
  97.             /// 
  98.             /// TreeNodeData返回TreeNode
  99.             /// 
  100.             /// 
  101.             public TreeNode ToTreeNode()
  102.             {
  103.                 TreeNode ToTreeNode = new TreeNode(this.Text, this.ImageIndex, this.SelectedImageIndex);
  104.                 ToTreeNode.Checked = this.Checked;
  105.                 ToTreeNode.Tag = this.Tag;
  106.                 if (this.Expanded) 
  107.                 {
  108.                     ToTreeNode.Expand();
  109.                 }
  110.                 if (this.Nodes == null && this.Nodes.Length == 0) 
  111.                 {
  112.                     return null;
  113.                 }
  114.                 if(ToTreeNode != null && this.Nodes.Length == 0)
  115.                 {
  116.                     return ToTreeNode;
  117.                 }
  118.                 for (int i = 0; i <= this.Nodes.Length - 1; i++) 
  119.                 {
  120.                     ToTreeNode.Nodes.Add(this.Nodes[i].ToTreeNode());
  121.                 }
  122.                 return ToTreeNode;
  123.             }
  124.         }
  125.         /// 
  126.         /// 加载TreeView
  127.         /// 
  128.         /// 
  129.         /// 
  130.         public static void LoadTreeViewData(TreeView treeView, string path)
  131.         {
  132.             BinaryFormatter ser = new BinaryFormatter();
  133.             Stream file = new FileStream(path,FileMode.Open,FileAccess.Read,FileShare.Read);
  134.             TreeViewData treeData = ((TreeViewData)(ser.Deserialize(file)));
  135.             treeData.PopulateTree(treeView);
  136.             file.Close();
  137.         }
  138.         /// 
  139.         /// 保存TreeView到文件
  140.         /// 
  141.         /// 
  142.         /// 
  143.         public static void SaveTreeViewData(TreeView treeView, string path)
  144.         {
  145.             BinaryFormatter ser = new BinaryFormatter();
  146.             Stream file = new FileStream(path,FileMode.Create);
  147.             ser.Serialize(file,new TreeViewData(treeView));
  148.             file.Close();
  149.         }
  150.     }
  151. }

你可能感兴趣的:(C#编程资料)