M
M
MEIION2018-02-11 18:53:35
C++ / C#
MEIION, 2018-02-11 18:53:35

How to write the paths of all files and folders to a txt file and load it into TreeView?

Help, you need 2 methods, the first one that saves all the paths of files and folders into a text file (not in xml), so that it turns out something like this, only with files

E:\temp
E:\temp\1
E:\temp\1\11
E:\temp\1\11\111
E:\temp\1\12
E:\temp\2
E:\temp\2\21
E:\temp\2\22
E:\temp\2\22\222
E:\temp\3

And the second one is to read this document and build a TreeView tree based on it.
Thank you in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan Arxont, 2018-02-12
@MEIION

Write a list of files and folders to a file

private void FoldersToFile()
{
    var folder = @"C:\Temp";
    var fileResult = @"X:\Temp\result.txt";

    TextWriter tw = new StreamWriter(fileResult, false, Encoding.Default);

    tw.WriteLine(folder);

    Directory.GetDirectories(folder, "*", SearchOption.AllDirectories)
                     .ToList().ForEach(dir =>
    {
        tw.WriteLine(dir);
        Directory.GetFiles(dir).ToList().ForEach(file => tw.WriteLine(file));
    });

    tw.Close();
}

Score in treeView
private void ReadFile()
{
    var fileResult = @"X:\Temp\result.txt";
    var content = File.ReadAllLines(fileResult).ToList();
    
    foreach (var element in content)
    {
        if (treeView1.Nodes.Count >0)
        {
            var x = SearchNode(Directory.GetParent(element).ToString(), treeView1.Nodes[0]);
            x?.Nodes.Add(element);
        }
        else treeView1.Nodes.Add(element);
    }
    treeView1.ExpandAll();
}

5a8133a095293277111782.png

M
MIsternik, 2018-02-11
@MIsternik

Have you tried writing it yourself? Show what didn't work.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question