-1

I have a treeview and I want to export treeview parent nodes, child to .txt file. but when I export .txt file the format looks very bad. I found this topic but its not helpful for me. There is a another way ?

Saving content of a treeview to a file and load it later

I want to like this in .txt file

  1. Main Folder
    • Sub Folder
      • .docx or .zip file
      • .docx or .zip file 2
    • Sub Folder 2
      • .txt file

but in my .txt file like this.

ÿÿÿÿ WSystem.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 ›System.Collections.Generic.List`1[[System.Windows.Forms.TreeNode, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] _items_size_version System.Windows.Forms.TreeNode[] System.Windows.Forms.TreeNode
System.Windows.Forms.TreeNode TextToolTipTextName IsChecked ImageIndexImageKeySelectedImageIndexSelectedImageKey ChildCount children0 children1 children2 children3 children4 children5 children6 children7 children8 children9 children10 children11 children12 children13 children14 children15 children16 children17 children18 children19 children20 children21UserData System.Windows.Forms.TreeNode System.Windows.Forms.TreeNode System.Windows.Forms.TreeNode System.Windows.Forms.TreeNode System.Windows.Forms.TreeNode System.Windows.Forms.TreeNode System.Windows.Forms.TreeNode System.Windows.Forms.TreeNode System.Windows.Forms.TreeNode System.Windows.Forms.TreeNode System.Windows.Forms.TreeNode System.Windows.Forms.TreeNode System.Windows.Forms.TreeNode System.Windows.Forms.TreeNode System.Windows.Forms.TreeNode System.Windows.Forms.TreeNode System.Windows.Forms.TreeNode System.Windows.Forms.TreeNode System.Windows.Forms.TreeNode System.Windows.Forms.TreeNode System.Windows.Forms.TreeNode System.Windows.Forms.TreeNode +ASP .NET MVC Yazılımcıların Yükselişi

IC:\Users\asd\Desktop\ASP .NET MVC Yazılımcıların Yükselişi System.Windows.Forms.TreeNode TextToolTipTextName IsChecked ImageIndexImageKeySelectedImageIndexSelectedImageKey ChildCount children0 children1 children2 children3 children4UserData System.Windows.Forms.TreeNode System.Windows.Forms.TreeNode System.Windows.Forms.TreeNode System.Windows.Forms.TreeNode System.Windows.Forms.TreeNode 1-ASP.NET MVC Giriş ! " # $ % ^C:\Users\asd\Desktop\ASP .NET MVC

this my code

public static void SaveTree(TreeView tree, string filename)
    {
        using (Stream file = File.Open(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\myFile.txt.", FileMode.Create))
        {
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(file, tree.Nodes.Cast<TreeNode>().ToList());
        }
    }
Oğuz Özdemir
  • 89
  • 3
  • 12

1 Answers1

3

To write the tree View in the Text File , you can go through the following code

    private void button1_Click(object sender, EventArgs e)
    {
        btnCreateTreeData();
    }

    private void btnCreateTreeData()
    {
        // create buffer for storing string data
        System.Text.StringBuilder buffer = new System.Text.StringBuilder();
        // loop through each of the treeview's root nodes
        foreach (TreeNode rootNode in treeView1.Nodes)
            // call recursive function
            BuildTreeString(rootNode,buffer);
        // write data to file
        System.IO.File.WriteAllText(@"D:\treeTest.txt", buffer.ToString());
    }

    private void BuildTreeString(TreeNode rootNode,System.Text.StringBuilder buffer)
    {
        buffer.Append(rootNode.Text);
        buffer.Append(Environment.NewLine);
        foreach (TreeNode childNode in rootNode.Nodes)
            BuildTreeString(childNode,buffer);
    }

Tree View in Form Tree View Saved in .txt File

SH7
  • 732
  • 7
  • 20
  • why ref StringBuilder? isn't a StringBuilder reference type already? – Mihir Dave Oct 31 '18 at 12:06
  • Thanks for your help. its works for me. I add this line for your code. There is a short way ? foreach (TreeNode childNode in rootNode.Nodes) { if(childNode.Level == 1) { buffer.Append("\t"); } else if(childNode.Level == 2) { buffer.Append("\t\t"); } – Oğuz Özdemir Oct 31 '18 at 12:42