Reputation: 301
After scouring the internet for a way to do this that was a lite as possible, I was not able to find something that was working for me. I have taken the coding from this question in an attempt to make it work but cannot figure out how to save my life. Here is what I have:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Get a list of drives
Dim drives As System.Collections.ObjectModel.ReadOnlyCollection(Of DriveInfo) = My.Computer.FileSystem.Drives
Dim rootDir As String = String.empty
'Now loop thru each drive and populate the treeview
For i As Integer = 0 To drives.Count - 1
rootDir = drives(i).Name
'Add this drive as a root node
TreeView1.Nodes.Add(rootDir)
'Populate this root node
PopulateTreeView(rootDir, TreeView1.Nodes(i))
Next
End Sub
Private Sub PopulateTreeView(ByVal dir As String, ByVal parentNode As TreeNode)
Dim folder As String = String.Empty
Try
Dim folders() As String = IO.Directory.GetDirectories(dir)
If folders.Length <> 0 Then
Dim childNode As TreeNode = Nothing
For Each folder In folders
childNode = New TreeNode(folder)
parentNode.Nodes.Add(childNode)
PopulateTreeView(folder, childNode)
Next
End If
Catch ex As UnauthorizedAccessException
parentNode.Nodes.Add(folder & ": Access Denied")
End Try
End Sub
However it only lists the drives & their folders and I need it to list the directories and files from a specific directory right from the beginning without having to go all the way down the tree to find what you are specifically looking for. I have tried changing rootDir
to the address of the specific folder that I am trying to access, but to no avail. Does anyone have any suggestions?
Upvotes: 0
Views: 3143
Reputation: 4593
I'm not sure I understand the question, but based on my comment in the original question: this code will do what I said in that comment. It's written in C# but here is a converter. Also: it's probably not production ready, but it's a start.
private void Form1_Load(System.Object sender, System.EventArgs e)
{
//Get a list of drives
DriveInfo[] drives = DriveInfo.GetDrives();
string rootDir = string.Empty;
//Now loop thru each drive and populate the treeview
for (int i = 0; i <= drives.Length - 1; i++)
{
rootDir = drives[i].Name;
//Add this drive as a root node
TreeView1.Nodes.Add(rootDir);
//Populate this root node
PopulateTreeView(rootDir, TreeView1.Nodes[i]);
}
}
private void PopulateTreeView(string dir, TreeNode parentNode)
{
string folder = string.Empty;
try
{
string[] folders = System.IO.Directory.GetDirectories(dir);
if (folders.Length != 0)
{
TreeNode childNode = null;
foreach (string folder_loopVariable in folders)
{
folder = folder_loopVariable;
childNode = new TreeNode(folder);
childNode.Nodes.Add("");
parentNode.Nodes.Add(childNode);
}
}
string[] files = System.IO.Directory.GetFiles(dir);
if (files.Length != 0)
{
TreeNode childNode = null;
foreach (string file in files)
{
childNode = new TreeNode(file);
parentNode.Nodes.Add(childNode);
}
}
}
catch (UnauthorizedAccessException ex)
{
parentNode.Nodes.Add(folder + ": Access Denied");
}
}
private void TreeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
{
TreeNode actual = e.Node;
actual.Nodes[0].Remove();
PopulateTreeView(actual.Text, actual);
}
private void TreeView1_BeforeCollapse(object sender, TreeViewCancelEventArgs e)
{
TreeNode actual = e.Node;
actual.Nodes.Clear();
actual.Nodes.Add("");
}
Upvotes: 2