This question already has an answer here:
Below is my code
DirectoryInfo directoryInfo = new DirectoryInfo(@"C:\Users\Shahul\Documents\Visual Studio 2010\Projects\TreeView\TreeView\bin\FileExplorer");
private void Form1_Load(object sender, EventArgs e)
{
if (Directory.Exists("FileExplorer"))
{
try
{
DirectoryInfo[] directories = directoryInfo.GetDirectories();
foreach (FileInfo file in directoryInfo.GetFiles())
{
if (file.Exists)
{
TreeNode nodes = treeView.Nodes[0].Nodes.Add(file.Name);
}
}
if (directories.Length > 0)
{
foreach (DirectoryInfo directory in directories)
{
TreeNode node = treeView.Nodes[0].Nodes.Add(directory.Name);
node.ImageIndex = node.SelectedImageIndex = 0;
foreach (FileInfo file in directory.GetFiles())
{
if (file.Exists)
{
TreeNode nodes = treeView.Nodes[0].Nodes[node.Index].Nodes.Add(file.Name);
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
When I run I just get a blank treeview form? Unable to figure out what is the error?
Btw this my first post in Stack Overflow.
Try this: (note make sure your directoryInfo location contains some folders)
This should solve your problem, I tried on WinForm though:
It is a simple example of how you can open file in rich text box, it can be improved a lot :). You might want to mark as answer or vote up if it helped :) !!
Try the following:
It's better to split out the directory parsing into a recursive method so that you can go all the way down the tree.
This WILL block the UI until it's completely loaded - but fixing that is beyond the scope of this answer...
:)
DirectoryInfo.Exists("FileExplorer") will check for "C:\Users\Shahul\Documents\Visual Studio 2010\Projects\TreeView\TreeView\bin\debug\FileExplorer", not "C:\Users\Shahul\Documents\Visual Studio 2010\Projects\TreeView\TreeView\bin\FileExplorer", when you are running in debug mode.