I'm new to VB and I'm hoping someone can help with the first major problem I've encountered.
I've created a form, which:
- Allows me to specify a SearchString in a text box
- To specify a FolderPath using FolderBrowserDialog in a text box
- Pass the values in the text boxes as variables
- Return all files in the FolderPath, containing the SearchString, with wildcards, to a ListBox when a button is clicked.
The code behind the button is as follows:
Private Sub ListButton_Click(sender As Object, e As EventArgs) Handles ListButton.Click
Dim fls
Dim FolderPath As String
Dim SearchString As String
FolderPath = FolderPathBox.Text
SearchString = SearchStringBox.Text
fls = My.Computer.FileSystem.GetFiles(FolderPath,"*" & SearchString & "*")
For Each f As String In fls
MatchingFilesBox.Items.Add(f)
Next
End Sub
However, after populating the SearchString and FolderPath text boxes with the following values respectively:
(1)
C:\Backup\Files
and clicking the button, the following error is returned:
Additional information: Conversion from string "* (1)*" to type 'Integer' is not valid.
The same error is displayed even if I do not specify a number e.g. "an" and I've not specifically configured any text boxes or classes or variables as data type integer.
I've simplified the code by removing the variables and wildcards from the equation and hard coding the path and a file name:
'fls = My.Computer.FileSystem.GetFiles(FolderPath,"*" & SearchString & "*")
fls = My.Computer.FileSystem.GetFiles("C:\Backup\Files", "abandoning.docx")
But the same error on converting to data type integer is displayed:
Additional information: Conversion from string "abandoning.docx" to type 'Integer' is not valid.
I'm flummoxed as to why or how an integer is being passed or retrieved in the file path. I've searched for answers to the error, but the articles I've read relate to number values, while mine doesn't; or to empty textboxes, which I believe I've eliminated; or use Replace, which I'm not.
Can anyone offer any guidance on overcoming this issue, so I can return all files in a folder containing a specific string in the filename?
GetFiles()does not have an overload that takes 2 strings.