Save knots from wood

I am using a treeview control in my windows application. This application has a button that adds several nodes (root node and child node). Now I want to save this structure and use it when I open the application again.

How to do it?

+1
source share
1 answer

You need to do the following:

1- Serialize your tree constructor using BinaryFormatter, as a starting point see below

private Byte[] SerilizeQueryFilters()
    {
        BinaryFormatter bf = new BinaryFormatter();

        TreeNodeCollection tnc = treeView1.Nodes;

        List<TreeNode> list = new List<TreeNode>();
        list.Add(treeView1.Nodes[0]);


        using (MemoryStream ms = new MemoryStream())
        {
            bf.Serialize(ms, list);
            return ms.GetBuffer();

        }


    }

2- Once you get an array of bytes, you can either save it in a database or in a file.

3. , , , strore byte [], , -.

4- , deserielize

 private void DeSerilizeQueryFilters(byte[] items)
    {
        BinaryFormatter bf = new BinaryFormatter();

        List<TreeNode> _list = new List<TreeNode>();

        try
        {
            using (MemoryStream ms = new MemoryStream())
            {
                ms.Write(items, 0, items.Length);
                ms.Position = 0;

                _list = bf.Deserialize(ms) as List<TreeNode>;



            }


        }
        catch (Exception ex)
        {
        }




    }

, _list node, , , .

0

All Articles