Combobox to populate with Folder list

zendog1960

Member
Joined
Jan 9, 2008
Messages
19
Programming Experience
Beginner
Morning all!

I have a winform that has one combobox. I want to populate that it with a list of folders in a directory. I thought this might be a common thing but I have not found any thing concerning this.

For this example lets say the root folder is C:\Testing\Folder List\

In the folder "Folder List" directory there are 5 folders as such:

Folder 1
Folder 2
Folder 3
Folder 4
Folder 5

I would like the combobox to reflect the names of the folders, i.e Folder 1 thru Folder 5. This must be somewhat dynamic in that users may create and delete folders in the "Folder List" folder daily so at runtime this needs to go get the current list from the location.

I for the life of me cannot find anything to help me with this. Keep in mind I am learning all this as I go so be gentle!

Thank you all for your quick responses and help!
 
VB.NET:
Me.FoldersComboBox.DataSource = (From folder In New IO.DirectoryInfo("C:\Testing\Folder List\").GetDirectories Select (folder.Name)).ToArray
You can find file and folder tools in System.IO namespace.
 
Thanks John.

I actually can up with this code and it works. Seems to be longer than your however.

VB.NET:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ' Make a reference to a directory.
        Dim di As New DirectoryInfo("C:\Testing\Folder List\")
        ' Get a reference to each directory in that directory.
        Dim diArr As DirectoryInfo() = di.GetDirectories()
        ' Populate Combobox.
        Dim dri As DirectoryInfo
        For Each dri In diArr
            cboxClient.Items.Add(dri.Name)
        Next dri
End Sub
 

Latest posts

Back
Top