Hey,
I'm having some trouble with list boxes at the moment. I need a list box to display the names of directories that are in certain a folder. How can I do this? Thanks in advance!
Printable View
Hey,
I'm having some trouble with list boxes at the moment. I need a list box to display the names of directories that are in certain a folder. How can I do this? Thanks in advance!
vb.net Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click For Each folder As String In IO.Directory.GetDirectories("c:\red_bull_NRG") ListBox1.Items.Add(IO.Path.GetFileName(folder)) Next End Sub
First issue is related to directories so first search in forums how you can find directories in particular folder.
You can use the IO.Directory.GetDirectories() method for that, like this:
As you can see from the arguments I have passed in, I am just setting it to search the root of the C drive for folders and I have told it to not search subdirectories too.vb Code:
For Each Directory As String In IO.Directory.GetDirectories("C:\", "*", IO.SearchOption.TopDirectoryOnly) MyListBox.Items.Add(Directory) Next
There is an equivalent method for files (unsurprisingly called GetFiles) too. I think both are available in My.Computer.FileSystem as well as the IO namespace
EDIT: Damn Hack beat me to it :)