Serialization of the TreeView of a tree control

Source: Internet
Author: User
Tags bool serialization
treeview| Control
Recently made a small software, a file browser, compared to Windows Explorer, you can view the contents of the file, rather than external programs open, support a variety of file formats, and later prepare to join the content-based search (intended to use the open source of the Dotlucene component). It needs to serialize the tree control, look up a lot of information, in CodeProject find a vb.net write, find a code converter into C #, modified a bit, changed to binary serialization. Attached below for the necessary reference.

Original address:

Http://www.codeproject.com/vb/net/TreeViewDataAccess.asp



I have modified the C # source code:

Using System;
Using System.IO;
Using System.Windows.Forms;
Using System.Runtime.Serialization.Formatters.Binary;

Namespace Testall
{
<summary>
TreeView Serialization Class
</summary>
public class Treeviewdataaccess
{
Public treeviewdataaccess () {}

<summary>
Treeviewdata
</summary>
[Serializable ()]
public struct Treeviewdata
{
Public treenodedata[] Nodes;

<summary>
Recursive initialization of TreeView data
</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>
Pop the TreeView through Treeviewdata
</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 Constructors
</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 return to 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>
Load 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>
Save TreeView to File
</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 ();

}
}


}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.