[2005] File Search Questions
After much reading of many many post here and the help files in VB express, I have the following codes and would like to know if you think the 'directory' and 'file' methods are better than the 'directoryinfo' and 'fileinfo' I used.
Also, I tried to update the lblTotalFiles and lstFullNames as I stepped through but instead it waited until the end and populated all at once. Why is that?
VB Code:
Imports System
Imports System.IO
Imports Microsoft.VisualBasic.ControlChars
Imports System.Windows.Forms
Public Class frmMain
Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
Dim sPath As String = ""
Dim fbdPath As New FolderBrowserDialog()
fbdPath.ShowDialog()
With fbdPath
sPath = .SelectedPath
lblInitPath.Text = .SelectedPath.ToString
MsgBox(sPath)
End With
GatherFiles(sPath)
End Sub
Private Sub GatherFiles(ByVal sFilePath As String)
Dim instance As New DirectoryInfo(sFilePath)
Dim returnValue As FileInfo
Dim intValue As Integer = 0
For Each returnValue In instance.GetFiles("*.pdf", IO.SearchOption.AllDirectories)
lstFullNames.Items.Add(returnValue.FullName)
intValue = intValue + 1
Next returnValue
lblTotalFiles.Text = intValue.ToString
End Sub
End Class
Re: [2005] File Search Questions
If all you need are the names of the files and folders then you should use the File and Directory classes because they don't require instances to be created. There's no point creating FileInfo and DirectoryInfo objects when they aren't required.
The reason that your UI doesn't update until the end is that the process is too busy doing the searching to redraw the controls. You may want to think about using a BackgroundWorker to perform the long-running operation.
Also, take a look at your post after you submit it to make sure it looks OK. You could have easily gone back and edited your incorrect VBCODE tags.
Re: [2005] File Search Questions
Its ok, I updated the post and corrected the vbcode tags. Looks so much better now. :)
Re: [2005] File Search Questions
So what do you mean a background worker? Should I have a progress bar running and then when finished processing then show the results?
Re: [2005] File Search Questions