Searching for file names is a common task in computer programming and in Visual Basic you can easily add filenames in a particular path to a combo box. The "GetFiles" method returns the names of files including their paths in a specified directory. Once you have the path and file names, you can extract the file name by using the "GetFileName" method which returns the file name and extension of a specified path string.
- Skill level:
- Moderately Easy
Other People Are Reading
Launch Microsoft Visual Basic Express, click "New Project" on the left pane of your computer screen, and double-click "Windows Forms Application" to create a new project.
Double-click "ComboBox" from the "Toolbox" pane to create a new combo box control. Double-click "Button" to create a new button.
Double-click "Button1" to create a click event procedure. Type the following code in the very first line of the module to use the "System.IO" namespace:
Type the following inside the "Button_Click" procedure to create your variables:
Dim filenames() As String
Dim result As String
Get the path and files names in "C:\" by adding the following line of code:
fileNames = System.IO.Directory.GetFiles("C:\", ".")
Create a loop and add all the file names to the combo box control by adding the following code:
For Each file In filenames
result = Path.GetFileName(file) Me.ComboBox1.Items.Add(result) Next
Press "F5" to run your program and click "Button1" to fill the combo box with file names.
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for