Reputation: 1
I am new to VB.NET and I am having issues extracting the file name instead of the full path.
I tried using substring to parse out after "\" but cannot use it because of readOnlyCollectio(of String)
Private Sub FindInFiles_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles findInFiles.Click
' Retrieves the path of the path selected by user.
Dim myDocumentsPath As String = tree.SelectedNode.FullPath
' Look for the string "Visual Basic" in all document files in this
' directory tree, in case-insensitive mode.
Dim files As System.Collections.ObjectModel.ReadOnlyCollection(Of String) =
My.Computer.FileSystem.FindInFiles(MyApp.LastPath, txtFindText.Text, True,
FileIO.SearchOption.SearchAllSubDirectories)
' Show all file names in a listbox.
ListBox1.Items.Clear()
For Each file As String In files
ListBox1.Items.Add(file)
Upvotes: 0
Views: 500
Reputation: 84
First you want to Import System
Imports System
You will want to use the
IO.Path.GetFileName()
or if you want the Filename without Extension you do
IO.Path.GetFileNameWithoutExtension()
Upvotes: 2
Reputation: 11773
Take a look at this method
Private Function GetFilesWithText(path As String,
txtToFind As String) As List(Of String)
Dim rv As New List(Of String)
rv = My.Computer.FileSystem.FindInFiles(path,
txtToFind,
True,
FileIO.SearchOption.SearchAllSubDirectories).ToList()
For x As Integer = 0 To rv.Count - 1
rv(x) = IO.Path.GetFileName(rv(x))
' OR
'rv(x) = IO.Path.GetFileNameWithoutExtension(rv(x))
Next
Return rv
End Function
to use it
ListBox1.Items.Clear()
ListBox1.Items.AddRange(GetFilesWithText(MyApp.LastPath, txtFindText.Text).ToArray)
Upvotes: 0