[RESOLVED] Directory Items TO Listbox Items
Any help would be greatly appreciated. I'm making an application that does some stuff involving the given subject, so this is very crucial to the completion and efficiency of my application.
What I'm in need of today is simple (well, "seems" simple). I have one listbox that I want to populate with directories file's names. I don't mean the files inside the directories, but the names of the folders inside the directory.
So, I have "c:\temp\dog", I'd click on my button or whatever and search for the directory and I choose "temp", said listbox would show "dog".
Thanks, VBForums.
UPDATE: Oh, BTW. It doesn't HAVE to be a listbox, it can be anything (IE: Textbox, Labels, Etc.).
Re: Directory Items TO Listbox Items
Hey,
Have you looked at the FolderBrowserDialog Class:
http://msdn.microsoft.com/en-us/libr...serdialog.aspx
Also, you might want to look at the System.IO.Directory Class:
http://msdn.microsoft.com/en-us/libr...y_members.aspx
There are members in there for doing exactly what you want.
Hope that helps!!
Gary
Re: Directory Items TO Listbox Items
''try to use
vb Code:
Dim di As New IO.DirectoryInfo("c:\temp")
'' for adding directories
For Each d As IO.DirectoryInfo In di.GetDirectories
ListBox1.Items.Add(d.Name)
Next
'' for adding files
For Each f As IO.FileInfo In di.GetFiles
ListBox1.Items.Add(f.Name)
Next
Re: Directory Items TO Listbox Items
Something like
vb.net Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each FolderName As String In IO.Directory.GetDirectories("D:\", "*")
ListBox1.Items.Add(FolderName)
Next
End Sub
Re: Directory Items TO Listbox Items
Quote:
Originally Posted by
softkans
''try to use
vb Code:
Dim di As New IO.DirectoryInfo("c:\temp")
'' for adding directories
For Each d As IO.DirectoryInfo In di.GetDirectories
ListBox1.Items.Add(d.Name)
Next
'' for adding files
For Each f As IO.FileInfo In di.GetFiles
ListBox1.Items.Add(f.Name)
Next
You think this might do?:
Code:
Dim gen
Dim = inputbox("Directory Input:")
Dim di As New IO.DirectoryInfo(gen)
'' for adding directories
For Each d As IO.DirectoryInfo In di.GetDirectories
ListBox1.Items.Add(d.Name)
Next
'' for adding files
For Each f As IO.FileInfo In di.GetFiles
ListBox1.Items.Add(f.Name)
Next
Re: Directory Items TO Listbox Items
Hey,
What are you expecting that code to do?
I don't think it will even compile.
I think what you are after is this:
Code:
Dim gen As String
gen = inputbox("Directory Input:")
Dim di As New IO.DirectoryInfo(gen)
'' for adding directories
For Each d As IO.DirectoryInfo In di.GetDirectories
ListBox1.Items.Add(d.Name)
Next
'' for adding files
For Each f As IO.FileInfo In di.GetFiles
ListBox1.Items.Add(f.Name)
Next
Having said that though, why are you using an InputBox rather than a FolderBrowserDialog? Using an InputBox is prone to spelling mistakes etc.
Also, you original post didn't mention looping through the files in the folder, just the directories. What exactly is it you are wanting?
Hope that helps!!
Gary
Re: Directory Items TO Listbox Items
How could I substitute a inputbox with a folderbrowsedialog?
I'm trying to get all folder names and get them inside a listbox. Basically, every "dir" name becomes a line in the listbox.
UPDATE: I tried your code, it worked perfectly. Thread resolved. Although, I'm still interested in finding out the question to my answer above. :P
Re: Directory Items TO Listbox Items
All folder names on the entire drive?
That wasn't the original question. Are you changing it now?