树形控件TreeView的序列化
2024-07-21 02:16:04
供稿:网友
最近自己做了一个小软件,一个文件浏览器,与windows的资源管理器相比,可以查看文件内容,而不用外部程序打开,支持多种文件格式,以后准备加入基于内容的搜索(打算用开源的dotlucene组件)。其中需要将树形控件序列化,查了很多资料,在codeproject找到一个vb.net写的,找了一个代码转换器转成了c#,修改了一下,改成了二进制序列化。贴在下面供需要的参考。
原文地址:
http://www.codeproject.com/vb/net/treeviewdataaccess.asp
我修改过的c#源码:
using system;
using system.io;
using system.windows.forms;
using system.runtime.serialization.formatters.binary;
namespace testall
{
/// <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();
}
}
}