Directory to TreeView
This code loads the files and the directories of a directory into a treeview, here an example of how to use it
Code:
/*
* Coded by Sam
*/
private void Form1_Load(object sender, EventArgs e)
{
DirToTree("dirpath", treeViewVar);
}
public void DirToTree(string p, TreeView f, TreeNodeCollection c = null)
{
if (Directory.Exists(p))
{
if (c == null)
c = f.Nodes;
foreach (string s in Directory.GetDirectories(p))
{
if (new FileInfo(s).Attributes.HasFlag(FileAttributes.System)) continue;
c.Add(Path.GetFileName(s));
DirToTree(s, f, c[c.Count - 1].Nodes);
}
foreach (string s in Directory.GetFiles(p))
{
if (new FileInfo(s).Attributes.HasFlag(FileAttributes.System)) continue;
c.Add(Path.GetFileName(s));
}
}
}