0

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?

Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
Rachel Dockter
  • 946
  • 1
  • 8
  • 21
  • *"im trying to convert a tree view to a byte array and then back again"* - this is called serialization. Binary serialization is the worst choice as well as you can't use it to directly serialize state of complicated control like `TreeView`. Consider to: 1) use another serialization (XmlSerializer, json) 2) to save state - convert data into something (e.g. `TreeView.Nodes` -> hierarchical `List`) and serialize that, to restore - first read something and then add items one by one. – Sinatr Apr 01 '16 at 09:29
  • All visual stuff like `TreeNode` isn't a good choice for any persistence, as like as `BinaryFormatter` isn't a good choice for implementing persistence using serialization/deserialization. It is better to have your own DTO/model classes, which can be easily serialized/deserialized by any API, that provides portable format (XML, JSON, etc). – Dennis Apr 01 '16 at 09:31
  • i probably should of said im not a good programmer and i dont really understand any of what was just said lol, i didnt make the code but thats the only code i could find. im not looking for 100% effective, full proof way, just something that works for now if you have that – Rachel Dockter Apr 01 '16 at 09:33
  • also i know u said binary is the worst but eventually its going to be sent over tcp so i think it has to be – Rachel Dockter Apr 01 '16 at 09:35

1 Answers1

0

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);
}
Visual Vincent
  • 18,045
  • 5
  • 28
  • 75