Visual C# Treeview question

I need a function to convert the treeview into lines of string
that can be saved into a text file and using notepad can still view
the structure of the treeview fully expanded.
sepknowAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sindhuxyzCommented:
Here is example to generate xml file from treeview, you can use it as you need

http://www.codeproject.com/KB/cpp/treeviewtoxml.aspx

http://www.codeproject.com/KB/cpp/TreeView_Serializer.aspx



Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics TeacherCommented:
Here's an old-school implementation that simply recurses the nodes depth first and outputs them using the correct number of Tabs based on their depth:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

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

        private void button1_Click(object sender, EventArgs e)
        {
            using (SaveFileDialog sfd = new SaveFileDialog())
            {
                sfd.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
                if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    try
                    {
                        SaveTreeView(treeView1, sfd.FileName);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("File: " + sfd.FileName + Environment.NewLine + Environment.NewLine + ex.ToString(), "Error Saving TreeView", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }

        private void SaveTreeView(TreeView tv, String FileName)
        {
            using (StreamWriter sw = new StreamWriter(FileName, false))
            {
                foreach (TreeNode RootNode in tv.Nodes)
                {
                    WriteNode(RootNode, sw);
                }
            }
        }

        private void WriteNode(TreeNode tn, StreamWriter sw)
        {
            if (tn.Level > 0)
            {
                sw.Write(new String((char)9, tn.Level));
            }
            sw.WriteLine(tn.Text);
            foreach (TreeNode child in tn.Nodes)
            {
                WriteNode(child, sw);
            }
        }

    }
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sepknowAuthor Commented:
Idle Mind solution is closest to what I need. Thanks.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.