Display file location in a listbox

  • Thread starter Thread starter Sid Williams
  • Start date Start date
S

Sid Williams

Guest
Hello Developers !

I have a project where i am searching for exe file, now i added a listbox to my form, i want that while searching the executable files, my program should automatically add found files in the listbox. I am attaching the screenshot of my program as well as the code, please help me out.

Option Strict On
Option Explicit On

Imports System.IO

Public Class Form1

Private safeFullPaths As New List(Of String)
Private driveOrFolder As String = String.Empty
Private userCancel As Boolean = False
Private scanException As String = String.Empty



Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load

btn_BeginScan.Enabled = False
btn_AbortScan.Enabled = False

With ProgressBar1
.Minimum = 0
.Maximum = 100
.Visible = False
End With

With bgw_ScanFiles
.WorkerReportsProgress = True
.WorkerSupportsCancellation = True
End With

lbl_ScanStatus.Text = ""

End Sub



Private Sub btn_SelectDriveOrFolder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btn_SelectDriveOrFolder.Click

btn_BeginScan.Enabled = False
lbl_ScanStatus.Text = ""

Using fbo As New FolderBrowserDialog
With fbo
.Description = "Select The Drive Or Folder To Scan"
.RootFolder = Environment.SpecialFolder.MyComputer
.ShowNewFolderButton = False
End With

If fbo.ShowDialog = Windows.Forms.DialogResult.OK Then
driveOrFolder = fbo.SelectedPath
btn_BeginScan.Enabled = True
lbl_ScanStatus.Text = "Selected Path: " & fbo.SelectedPath
End If
End Using

End Sub



Private Sub btn_BeginScan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btn_BeginScan.Click

scanException = ""
userCancel = False
btn_SelectDriveOrFolder.Enabled = False
btn_BeginScan.Enabled = False
lbl_ScanStatus.Text = "Reading Folder Paths..."

bgw_ScanFiles.RunWorkerAsync()

With ProgressBar1
.Style = ProgressBarStyle.Marquee
.Visible = True
End With

btn_AbortScan.Enabled = True

Me.Refresh()

End Sub



Private Sub btn_AbortScan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btn_AbortScan.Click

userCancel = True

With btn_AbortScan
.Enabled = False
.Refresh()
End With

End Sub



Private Sub bgw_ScanFiles_DoWork(ByVal sender As System.Object, _
ByVal e As System.ComponentModel.DoWorkEventArgs) _
Handles bgw_ScanFiles.DoWork

GenerateSafeFolderList(driveOrFolder)

If Not userCancel Then
bgw_ScanFiles.ReportProgress(0, safeFullPaths.Count.ToString("n0") & " Paths Found" & vbCrLf & _
"Scanning Files On All Paths...")

Dim totalCnt As Integer = safeFullPaths.Count
Dim loopCnt As Integer = 0
Dim filePaths As New List(Of String)

For Each p As String In safeFullPaths
If userCancel Then
bgw_ScanFiles.CancelAsync()
Exit Sub
Else
Try
For Each f As String In My.Computer.FileSystem.GetFiles(p, _
FileIO.SearchOption.SearchTopLevelOnly, "*.exe")

filePaths.Add(f)


Next



Catch ex As Exception
scanException = ex.Message
bgw_ScanFiles.CancelAsync()
Exit Sub
End Try

loopCnt += 1

If loopCnt Mod 7 = 0 OrElse loopCnt = totalCnt Then
Dim pctDone As Double = (loopCnt / totalCnt) * 100
If pctDone > 100 Then
Stop
End If
bgw_ScanFiles.ReportProgress(CInt(pctDone), loopCnt.ToString("n0") & " of " & _
totalCnt.ToString("n0") & vbCrLf & _
pctDone.ToString("f1") & "% Complete")
End If
End If
Next

e.Result = filePaths.Count


End If



End Sub



Private Sub bgw_ScanFiles_ProgressChanged(ByVal sender As Object, _
ByVal e As System.ComponentModel.ProgressChangedEventArgs) _
Handles bgw_ScanFiles.ProgressChanged

If e.ProgressPercentage > 0 Then
With ProgressBar1
.Style = ProgressBarStyle.Blocks
.Value = e.ProgressPercentage
.Refresh()
End With
End If

With lbl_ScanStatus
.Text = e.UserState.ToString
.Refresh()
End With

End Sub



Private Sub bgw_ScanFiles_RunWorkerCompleted(ByVal sender As Object, _
ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) _
Handles bgw_ScanFiles.RunWorkerCompleted

btn_AbortScan.Enabled = False
btn_SelectDriveOrFolder.Enabled = True

With ProgressBar1
.Value = 0
.Visible = False
End With

If scanException <> "" Then
lbl_ScanStatus.Text = "An error was encountered:" & vbCrLf & vbCrLf & scanException
ElseIf userCancel Then
lbl_ScanStatus.Text = "Operation aborted."
Else
Dim count As Integer = DirectCast(e.Result, Integer)
lbl_ScanStatus.Text = "Successfully completed." & vbCrLf & vbCrLf & _
"A total of " & count.ToString("n0") & " files scanned."
End If

End Sub



Private Sub GenerateSafeFolderList(ByVal folder As String)

-------------------------------------------
Based On A Function By John Wein As Posted:

http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/d6e64558-395b-4b48-8b64-0f5a7e3a7623

Thanks John!
-------------------------------------------

Dim Dirs As New Stack(Of String)
Dirs.Push(folder)

While Dirs.Count > 0
Dim Dir As String = Dirs.Pop

If Not userCancel Then
Try
For Each D As String In Directory.GetDirectories(Dir)
Do not include any that are either system or hidden

Dim dirInfo As New DirectoryInfo(D)
If (((dirInfo.Attributes And FileAttributes.Hidden) = 0) AndAlso _
((dirInfo.Attributes And FileAttributes.System) = 0)) Then

If Not safeFullPaths.Contains(D) Then
safeFullPaths.Add(D)
End If
End If

Dirs.Push(D)
Next

Catch ex As Exception

If safeFullPaths.Contains(Dir) Then
Dim indexToRemove As Integer = 0

For i As Integer = 0 To safeFullPaths.Count - 1
If safeFullPaths(i) = Dir Then
indexToRemove = i
Exit For
End If
Next

safeFullPaths.RemoveAt(indexToRemove)
End If
Continue While
End Try
End If
End While

End Sub



Private Delegate Sub GenerateSafeFolderListDelegate(ByVal folder As String)


End Class


346d16be1e92f5b8cbb820b4dd5d8dff._.png


Please help me out of how to do this.

Thanks

Continue reading...
 
Back
Top