Hi All,
I'vjust finished me aking a program which displays all the files in a
directory in a ListBox and lets me open them when selected. The problem I found with this was the files were hard to see what folder/subfolder they belonged too. So I'm trying to do the same thing with a treeview however I can't get the files to open from the subfolders.
Code so far:
string dir = @"C:\Test\VideoTraining\";
private void button1_Click(object sender, EventArgs e) // populate treeview
{
ListDirectory(treeView1, dir);
}
private void ListDirectory(TreeView treeview1, string dir)
{
treeView1.Nodes.Clear();
var rootDirectoryInfo = new DirectoryInfo(dir);
treeView1.Nodes.Add(CreateDirectoryNode(rootDirectoryInfo));
}
private static TreeNode CreateDirectoryNode(DirectoryInfo directoryInfo)
{
var directoryNode = new TreeNode(directoryInfo.Name);
foreach (var directory in directoryInfo.GetDirectories())
directoryNode.Nodes.Add(CreateDirectoryNode(directory));
foreach (var file in directoryInfo.GetFiles())
directoryNode.Nodes.Add(new TreeNode(file.Name));
return directoryNode;
}
private void button2_Click(object sender, EventArgs e) // to open file
{
string name = treeView1.SelectedNode.ToString().Replace("TreeNode: ",string.Empty);
MessageBox.Show(dir + "\\" + name);
System.Diagnostics.Process.Start(dir + "\\" + name);
}