Given a List of Strings to each file within the structure
var paths = new List<string> {"root_folder/a first folder/holidays.mov", "root_folder/a first folder/javascript-file.js", etc...}
I need to output a String that represents the directory structure
root_folder/
|-- a first folder/
| |-- holidays.mov
| |-- javascript-file.js
| `-- some_picture.jpg
|-- documents/
| |-- spreadsheet.xls
| |-- manual.pdf
| |-- document.docx
| `-- presentation.ppt
| `-- test
|-- empty_folder/
|-- going deeper/
| |-- going deeper/
| | `-- going deeper/
| | `-- going deeper/
| | `-- .secret_file
| |-- style.css
| `-- index.html
|-- music and movies/
| |-- great-song.mp3
| |-- S01E02.new.episode.avi
| |-- S01E02.new.episode.nfo
| `-- track 1.cda
|-- .gitignore
|-- .htaccess
|-- .npmignore
|-- archive 1.zip
|-- archive 2.tar.gz
|-- logo.svg
`-- README.md
I found some information for the
So, you can use shell() command, and capture the output of the windows command "tree", and it does quite a nice job.
It really depends on what you want to do with the final output.
Overall, it not a whole lot less code to use a treeview, since as such, they can be recursive.
CodePudding user response:
Managed to get the dir structure with this class i've made:
using System.Collections.Generic;
using System.Linq;
using System.Text;
/// <summary>
/// Summary description for CodeComparisonToolDirBuilder
/// </summary>
public class CodeComparisonToolDirBuilder
{
public string Name { get; private set; }
public CodeComparisonToolDirBuilder Parent { get; set; }
public string Hash { get; set; }
public bool Read { get; set; }
public bool Write { get; set; }
public List<CodeComparisonToolDirBuilder> Children { get; private set; }
public CodeComparisonToolDirBuilder(string name, CodeComparisonToolDirBuilder parent)
{
Name = name;
Parent = parent;
Children = new List<CodeComparisonToolDirBuilder>();
}
public bool Contains(string name)
{
return Children.Any(d => d.Name.Equals(name));
}
public CodeComparisonToolDirBuilder Get(string name)
{
return Children.FirstOrDefault(d => d.Name.Equals(name));
}
public override string ToString()
{
return Name;
}
public void PrintTree(StringBuilder output, bool isRoot)
{
string prefix;
string pre_0 = " ";
string pre_1 = "│ ";
string pre_2 = "|-- ";
string pre_3 = "`-- ";
CodeComparisonToolDirBuilder tree = this;
if (tree.Parent != null && !(tree.Equals(tree.Parent.Children.Last())))
{
prefix = pre_2;
}
else
{
prefix = pre_3;
}
while (tree.Parent != null && tree.Parent.Parent != null)
{
if (tree.Parent != null && !(tree.Parent.Equals(tree.Parent.Parent.Children.Last())))
{
prefix = pre_1 prefix;
}
else
{
prefix = pre_0 prefix;
}
tree = tree.Parent;
}
if (isRoot)
{
output.AppendLine(this.Name);
}
else
{
output.AppendLine(prefix this.Name);
}
foreach (CodeComparisonToolDirBuilder child in this.Children)
{
child.PrintTree(output, false);
}
}
public static void TreeStruct(CodeComparisonToolDirBuilder parent, List<string> edges)
{
if (edges.Count == 0) return;
List<CodeComparisonToolDirBuilder> matchedChildren = new List<CodeComparisonToolDirBuilder>();
foreach (CodeComparisonToolDirBuilder tree in parent.Children)
{
if (tree.Name == edges[0])
{
matchedChildren.Add(tree);
}
}
CodeComparisonToolDirBuilder pointer;
if (matchedChildren.Count != 0)
{
pointer = matchedChildren[0];
}
else
{
pointer = new CodeComparisonToolDirBuilder(edges[0], parent);
parent.Children.Add(pointer);
}
edges.RemoveAt(0);
TreeStruct(pointer, edges);
}
}
To use simply declare a CodeComparisonToolDirBuilder at the root folder/node. This was inspired by enter link description here