OK now I'm leaving the delete functionality out as an exercise for you.
When you click the stop button this code doesn't restart from the place it stopped at if you click on the search button again but from the beginning.
You will need on your form;
A button called searchButton
A button called stopButton
A TextBox called searchPath
A button called searchPathSelect
a Label called currentPath
and a listbox called ListBox1
************** All Code Updated *****************
Imports System.IO
Public Class Form1
Private Sub searchButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles searchButton.Click
If searchPath.Text <> String.Empty Then
Dim direc As New DirectoryInfo(searchPath.Text)
If direc.Exists = False Then
MsgBox("The path does not exist. Please specify a correct path")
Else
searchButton.Enabled = False
stopButton.Enabled = True
Application.DoEvents()
RepeatDir(direc)
RepeatFiles(direc)
MessageBox.Show("Scan Complete")
End If
Else
MessageBox.Show("Select a path to begin searching from.")
End If
End Sub
Private Sub stopButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles stopButton.Click
searchButton.Enabled = True
stopButton.Enabled = False
End Sub
Private Sub searchPathSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles searchPathSelect.Click
Dim browserDialog As New FolderBrowserDialog()
Dim dialogResult As DialogResult = browserDialog.ShowDialog()
If dialogResult = Windows.Forms.DialogResult.OK Then
searchPath.Text = browserDialog.SelectedPath
End If
End Sub
Function RepeatDir(ByVal drr As DirectoryInfo)
Dim directoryListing() As DirectoryInfo = drr.GetDirectories()
For Each fls As DirectoryInfo In directoryListing
Try
If searchButton.Enabled = False Then
RepeatDir(fls)
RepeatFiles(fls)
End If
Catch ex As Exception
ListBox1.Items.Add(ex.Message)
End Try
Next
End Function
Function RepeatFiles(ByVal dirs As DirectoryInfo)
Dim fileListing() As FileInfo = dirs.GetFiles()
If fileListing.Length > 0 Then
For Each fl As FileInfo In fileListing
If searchButton.Enabled = False Then
currentPath.Text = fl.FullName
Application.DoEvents()
Dim currentFileName As String = fl.Name
Dim fileExists As Boolean = My.Resources.VirusList.Contains(currentFileName)
If fileExists = True Then
If MessageBox.Show("File found do you want to delete it?", "File Found", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
ListBox1.Items.Add("Removing File: " & fl.Name)
fl.Delete()
End If
End If
End If
Next
End If
End Function
End Class