|
|
Title | List the files in a directory that match a pattern in VB .NET |
Description | This example shows how to list the files in a directory that match a pattern in VB .NET. |
Keywords | directory, list, files, list files, pattern, search, file search |
Categories | Files and Directories, VB.NET |
|
|
When you click the List button, the program reads a search pattern (e.g. *.txt) from a combo box. It makes a new DirectoryInfo object, passing its constructor the path to the directory to search. It calls the object's GetFiles method, passing it the search pattern. This method returns an array of FileInfo objects describing the files that match the pattern. The program loops through the array adding the files' names to a list box.
|
|
Private Sub btnList_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnList.Click
' Get the pattern without stuff in parens.
Dim pattern As String = cboPattern.Text
If pattern.IndexOf("(") >= 0 Then
pattern = pattern.Substring(0, pattern.IndexOf("("))
End If
' Get the files.
lstFiles.Items.Clear()
Dim dir_info As New DirectoryInfo(txtDir.Text)
Dim file_infos() As FileInfo
file_infos = dir_info.GetFiles(pattern)
For Each file_info As FileInfo In file_infos
lstFiles.Items.Add(file_info.Name)
Next file_info
End Sub
|
|
|
|
|
|