Search code examples
c#serializationtreeviewbinary-serialization

Converting tree view to byte[], then back again


im trying to convert a tree view to a byte array and then back again. so far when the form loads, it will load the structure of my documents. Then as far as i know it will convert it to a byte array and back but im not sure how to convert the byte array back to the tree view. Here is my code

namespace tree_view
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        string filepath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        ListDirectory(treeView1, filepath);
    }

    private static void ListDirectory(TreeView treeView, string path)
    {
        treeView.Nodes.Clear();

        var stack = new Stack<TreeNode>();
        var rootDirectory = new DirectoryInfo(path);
        var node = new TreeNode(rootDirectory.Name) { Tag = rootDirectory };
        stack.Push(node);

        while (stack.Count > 0)
        {
            var currentNode = stack.Pop();
            var directoryInfo = (DirectoryInfo)currentNode.Tag;
            foreach (var directory in directoryInfo.GetDirectories())
            {
                var childDirectoryNode = new TreeNode(directory.Name) { Tag = directory };
                currentNode.Nodes.Add(childDirectoryNode);
                stack.Push(childDirectoryNode);
            }
            foreach (var file in directoryInfo.GetFiles())
                currentNode.Nodes.Add(new TreeNode(file.Name));
        }

        treeView.Nodes.Add(node);
    }

    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();
        }
    }

    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>;
                treeView2 = _list;
            }
        }
        catch { }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        byte[] data = SerilizeQueryFilters();
        DeSerilizeQueryFilters(data);
    }
}

So the bit thats giving me the error at the moment is "treeView2 = _list;" as i "Cannot implicitly convert type System.collections.generic.List to System.Windows.Forms.TreeView". anyone have any ideas?


Solution

  • In your DeSerilizeQueryFilters() method you should be able to just call .AddRange() on your TreeView's Nodes collection and add all the nodes from your list.

    _list = bf.Deserialize(ms) as List<TreeNode>;
    treeView2.Nodes.AddRange(_list.ToArray()); //The parameter needs to be an array.
    

    EDIT:

    Also, in your SerilizeQueryFilters() method you're currently only adding the first TreeNode to your list. To add all of the nodes simply replace:

    list.Add(treeView1.Nodes[0]);
    

    with:

    foreach(TreeNode node in treeView1.Nodes)
    {
        list.Add(node);
    }