[RESOLVED] [2008] Getting a list of files with certain file types
Hi!
How do I get a list of files with a certain extension in a directory and return that list in a combobox?
e.g
Say I want to find all the text files in the "C:\" directory and return their names in a combobox, how would I go about doing it?
Any help is much appreciated.
Louix.
Re: [2008] Getting a list of files with certain file types
Code:
Dim di As New System.IO.DirectoryInfo("C:\")
Dim fi() As System.IO.FileInfo
fi = di.GetFiles("*.txt")
For ct As Integer = 0 To fi.Length - 1
Debug.WriteLine(fi(ct).FullName)
Next
Re: [2008] Getting a list of files with certain file types
Use the GetFiles function.
Code:
Option Explicit On
Option Strict On
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim xlFiles() As String
xlFiles = System.IO.Directory.GetFiles("C:\Users\UserName\Documents", "*.xls", IO.SearchOption.TopDirectoryOnly)
ComboBox1.Items.AddRange(xlFiles)
End Sub
End Class
Re: [2008] Getting a list of files with certain file types
If you want only the file names, rather than the full paths, displayed in the ComboBox you'd modify what's already been posted:
vb.net Code:
myComboBox.DisplayMember = "Name"
myComboBox.ValueMember = "FullName"
myComboBox.DataSource = (New IO.DirectoryInfo("folder path here")).GetFiles("*.txt")
By binding an array of FileInfo objects to the control you have access to the file name only as well as the full path. Setting the DisplayMember to "Name" will display just the file names in the control, while setting the ValueMember to "FullName" will return the full path of the selected file from the control's SelectedValue property.
Problem Now Solved! Thanks a lot guys! :D
Thanks sooooooooooo much guys you solved my problem and it now works exactly how I wanted it to!
Again, thanks so much!!!!!! :D
Louix.