Answer the question
In order to leave comments, you need to log in
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
Answer the question
In order to leave comments, you need to log in
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();
}
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();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question