adding files to listboxes

fresh1207

Member
Joined
Jan 5, 2007
Messages
6
Programming Experience
1-3
I have a program that will search directories and add files to a listbox by the extension you enter. I have a table created in access that contains files. The program will check the database to see if files exist. I have it where if the path in tblExclude is equal to the path textbox1 then add files to listbox 2 and all other files are added to listbox1. My problem is I don't want the files that are added to listbox2 to be added to textbox1. The files that are in tblExclude are being added to both listboxes instead of just listbox2. Can someone please help me with this? Here is my code:
VB.NET:
'Get the entered file paths and patterns
Dim enteredFilePaths As String() = TextBox1.Text.Split(New String() {";"}, StringSplitOptions.RemoveEmptyEntries)
Dim patterns As String() = TextBox2.Text.Split(New String() {";"}, StringSplitOptions.RemoveEmptyEntries)

'Declare a string array to store the file paths that are not found in the database
Dim filePaths() As String = Nothing
Dim dv As DataView = dt.DefaultView
dv.Sort = "ExcludePath ASC"

For Each path As String In enteredFilePaths

'Check to make sure the file path does not have "\" as it's last character.
If path.LastIndexOf("\") = path.Length - 1 Then
path = path.Substring(0, path.Length - 1)
End If

Dim rows As DataRowView() = dv.FindRows(path)

If rows.Length > 0 Then

'Only add the file if it is one of the entered extensions.
For Each row As DataRowView In rows
If ListBox2.Items.Contains(row.Item("ExcludeFileName")) Then

End If

Dim ext As String = System.IO.Path.GetExtension(row.Item("ExcludeFileName").ToString).Replace(".", "")
If Me.TextBox2.Text.IndexOf(ext, 0, StringComparison.CurrentCultureIgnoreCase) > -1 Then

'It was found in the database, display it in the listbox.
Me.ListBox2.Items.Add(row.Item("ExcludeFileName"))
End If
Next
Else
'It was not found in the database
Dim i2 As Integer = 0
If Not filePaths Is Nothing Then
i2 = UBound(filePaths) + 1
End If

'Resize the array
ReDim filePaths(i2)

'Add the path to the array
filePaths(i2) = path
End If

Next
GetDirectoryContents(enteredFilePaths, patterns)

End Sub

Private Sub GetDirectoryContents(ByVal dirs() As String, ByVal patterns() As String)
'Declare variable.
Dim dDir As DirectoryInfo

'Search directory for files and add to the listbox.
For Each sDir As String In dirs
If Not Directory.Exists(sDir) Then Continue For
dDir = New DirectoryInfo(sDir)
For Each ext As String In patterns
For Each fi As FileInfo In dDir.GetFileSystemInfos("*." & ext)
ListBox1.Items.Add(fi.Name)
Next
Next
Next
End Sub
 
Last edited by a moderator:
Back
Top