search for exe file from all drives

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

Sid Williams

Guest
Hello Developers !

I am working on a program that looks for exe files when we tell the program to search the exe files in a particular drive, now i want that i want to search for exe files from all accessible drives on my computer, I am attaching the code below, can someone help me out

Option Strict On

Imports

Imports System.Collections.ObjectModel
Imports System.Text
Imports System.Threading
Imports System.IO
Imports System.Security.Permissions

Public Class Form1

Dim DirectoriesSearched As Integer = 0

Dim SW As New Stopwatch

Dim QueuedDirectories As New List(Of String)
Dim SearchedDirectories As New List(Of String)
Dim AccessDeniedDirectories As New List(Of String)
Dim FileList As New List(Of String)

Threads

Dim Th1 As Thread
Dim Th2 As New Thread(AddressOf ThreadWork2)
Dim Th3 As New Thread(AddressOf ThreadWork3)
Dim Th4 As New Thread(AddressOf ThreadWork4)
Dim Th5 As New Thread(AddressOf ThreadWork5)
Dim Th6 As New Thread(AddressOf ThreadWork6)
Dim Th7 As New Thread(AddressOf ThreadWork7)
Dim Th8 As New Thread(AddressOf ThreadWork8)

Delegate Subs

Delegate Sub LabelTextDelegate(ByVal Label As Label, ByVal Text As String)
Delegate Sub ListboxItemAdder(ByVal Listbox As ListBox, ByVal Text As String)
Delegate Sub dClearListBox(ByVal Listbox As ListBox)
Delegate Sub ControlIsEnabled(ByVal control As Control, ByVal IsEnabled As Boolean)


Sub ChangeEnabled(ByVal Control As Control, ByVal IsEnabled As Boolean)
If Control.InvokeRequired = True Then
Control.Invoke(New ControlIsEnabled(AddressOf ChangeEnabled), Control, IsEnabled)
Else
Control.Enabled = IsEnabled
End If
End Sub


Sub ClearListbox(ByVal Listbox As ListBox)
If Listbox.InvokeRequired = True Then
Listbox.Invoke(New dClearListBox(AddressOf ClearListbox), Listbox)
Else
Listbox.Items.Clear()
End If
End Sub


Sub SetLabelText(ByVal Label As Label, ByVal Text As String)
If Label.InvokeRequired Then
Label.Invoke(New LabelTextDelegate(AddressOf SetLabelText), Label, Text)
Else
Label.Text = Text
End If
End Sub


Sub AddListboxItem(ByVal Listbox As ListBox, ByVal Text As String)
If Listbox.InvokeRequired Then
Listbox.Invoke(New ListboxItemAdder(AddressOf AddListboxItem), Listbox, Text)
Else
Listbox.Items.Add(Text)
End If
End Sub


<SecurityPermission(SecurityAction.Demand, Flags:=SecurityPermissionFlag.ControlThread)> _
Sub Threadwork1()
Thread.BeginThreadAffinity()
Th1.Priority = ThreadPriority.Highest
ChangeEnabled(ScanButton, False)
SetLabelText(CurrentOperationLabel, "Thread Started")
GetSearchablePaths()
SetLabelText(CurrentOperationLabel, "Scanning Files.")
GetFileList()
StatisticsRefresher.Enabled = False
SetLabelText(StatusLabel, "Search Complete.")
SetLabelText(CurrentOperationLabel, "Search Complete.")
ChangeEnabled(ScanButton, True)
SetLabelText(ScanThreadStateLabel, "Stopped")
Thread.EndThreadAffinity()
End Sub


<SecurityPermission(SecurityAction.Demand, Flags:=SecurityPermissionFlag.ControlThread)> _
Sub ThreadWork2()
Thread.BeginThreadAffinity()
Th2.Priority = ThreadPriority.Highest
ClearListbox(QueuedDirectoriesList)
Dim Tmp As List(Of String) = QueuedDirectories.ToArray.ToList
For Each S As String In Tmp
AddListboxItem(QueuedDirectoriesList, S)
Next
Thread.EndThreadAffinity()
End Sub



<SecurityPermission(SecurityAction.Demand, Flags:=SecurityPermissionFlag.ControlThread)> _
Sub ThreadWork3()
Thread.BeginThreadAffinity()
Th3.Priority = ThreadPriority.Highest
ClearListbox(SearchableDirectoriesList)
Dim Tmp As List(Of String) = SearchedDirectories.ToArray.ToList
For Each S As String In Tmp
AddListboxItem(SearchableDirectoriesList, S)
Next
Thread.EndThreadAffinity()
End Sub



<SecurityPermission(SecurityAction.Demand, Flags:=SecurityPermissionFlag.ControlThread)> _
Sub ThreadWork4()
Thread.BeginThreadAffinity()
Th4.Priority = ThreadPriority.Highest
ClearListbox(ProtectedDirectoriesList)
Dim Tmp As List(Of String) = AccessDeniedDirectories.ToArray.ToList
For Each S As String In Tmp
AddListboxItem(ProtectedDirectoriesList, S)
Next
Thread.EndThreadAffinity()
End Sub


<SecurityPermission(SecurityAction.Demand, Flags:=SecurityPermissionFlag.ControlThread)> _
Sub ThreadWork5()
Thread.BeginThreadAffinity()
Th5.Priority = ThreadPriority.Highest
ClearListbox(ScanResultsList)
Dim Tmp As List(Of String) = FileList.ToArray.ToList
For Each S As String In Tmp
AddListboxItem(ScanResultsList, S)
Next
Thread.EndThreadAffinity()
End Sub

<SecurityPermission(SecurityAction.Demand, Flags:=SecurityPermissionFlag.ControlThread)> _
Sub ThreadWork6()
Thread.BeginThreadAffinity()
Th6.Priority = ThreadPriority.Highest





Try
For Each item As String In ScanResultsList.Items


Dim company = FileVersionInfo.GetVersionInfo(item).CompanyName
Dim copyright = FileVersionInfo.GetVersionInfo(item).LegalCopyright
Dim trademark = FileVersionInfo.GetVersionInfo(item).LegalTrademarks
Dim ProductName = FileVersionInfo.GetVersionInfo(item).ProductName
Dim ProductVersion = FileVersionInfo.GetVersionInfo(item).ProductVersion
Dim FileBuildPart = FileVersionInfo.GetVersionInfo(item).FileBuildPart
Dim FileVersion = FileVersionInfo.GetVersionInfo(item).FileVersion


Dim FileBuild As String
FileBuild = FileBuildPart.ToString

If FileBuild = "0" Then
lblNullString.Text = ""
End If


If String.IsNullOrEmpty(company) And String.IsNullOrEmpty(copyright) And String.IsNullOrEmpty(trademark) And String.IsNullOrEmpty(ProductName) And String.IsNullOrEmpty(ProductVersion) And String.IsNullOrEmpty(FileVersion) Then


InfectedList.Items.Add(item)

End If
Next
Catch ex As Exception

End Try

lblInfections.Text = InfectedList.Items.Count.ToString
tmrConfirmInfections.Enabled = True

If InfectedList.Items.Count = 0 Then
lblAddressToSearch.Text = "No Virus Found"
End If


Thread.EndThreadAffinity()
End Sub


Sub GetFileList()
Do
GetFilesFromDirectory(SearchedDirectories.Item(0))
SetLabelText(FilesFoundLabel, CStr(FileList.Count))
SetLabelText(DirectoriesSearchedLabel, CStr(DirectoriesSearched))
Loop Until SearchedDirectories.Count = 0



Sub GetFilesFromDirectory(ByVal Folder As String)
Select Case FilesInDirectory(Folder).GetType
Case GetType(String())
FileList.AddRange(DirectCast(FilesInDirectory(Folder), String()).ToArray)
Case GetType(Boolean)
End Select
SearchedDirectories.RemoveAt(0)
DirectoriesSearched = DirectoriesSearched + 1
End Sub


Function FilesInDirectory(ByVal Folder As String) As Object
Try
Return Directory.GetFiles(Folder, "*.exe", SearchOption.TopDirectoryOnly)
Catch
Return False
End Try
End Function


Sub GetSearchablePaths()
Thread.BeginCriticalRegion()
SetLabelText(CurrentOperationLabel, "Getting Directories")
SearchLoop()
Thread.EndCriticalRegion()
End Sub


Sub GetSubDirectories(ByVal Directory As String)
Select Case ChildDirectories(Directory).GetType
Case GetType(String())
QueuedDirectories.AddRange(DirectCast(ChildDirectories(Directory), String()).ToArray)
SearchedDirectories.Add(Directory)
Case GetType(Boolean)
AccessDeniedDirectories.Add(Directory)
End Select
QueuedDirectories.RemoveAt(0)
End Sub


Sub SearchLoop()

For Each Drives In My.Computer.FileSystem.Drives
QueuedDirectories.Add(Drives.ToString)
Do
GetSubDirectories(QueuedDirectories(0))
SetLabelText(DirectoriesQueuedLabel, QueuedDirectories.Count.ToString)
SetLabelText(ProtectedDirectoriesLabel, AccessDeniedDirectories.Count.ToString)
SetLabelText(SearchableDirectoriesLabel, SearchedDirectories.Count.ToString)
Loop Until QueuedDirectories.Count = 0
Next

End Sub


Function ChildDirectories(ByVal Folder As String) As Object
Try
Return Directory.GetDirectories(Folder, "*", SearchOption.TopDirectoryOnly)
Catch
Return False
End Try
End Function







Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim fd As New FolderBrowserDialog
fd.ShowDialog()

If fd.SelectedPath <> "" Then
lblAddressToSearch.Text = fd.SelectedPath
Else
MsgBox("Operation cancelled by the user.", MsgBoxStyle.Information, "")
End If
End Sub



Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Process.GetCurrentProcess.PriorityClass = ProcessPriorityClass.High
End Sub



Private Sub ScanButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ScanButton.Click
Th1 = New Thread(AddressOf Threadwork1)
QueuedDirectories.Clear()
SearchedDirectories.Clear()
AccessDeniedDirectories.Clear()
FileList.Clear()
DirectoriesSearched = 0
ClearListbox(QueuedDirectoriesList)
ClearListbox(SearchableDirectoriesList)
ClearListbox(ProtectedDirectoriesList)
ClearListbox(ScanResultsList)
SetLabelText(ScanThreadStateLabel, "Unstarted")
SetLabelText(StatusLabel, "Awaiting Search Command")
SetLabelText(CurrentOperationLabel, "Idle")
SetLabelText(DirectoriesQueuedLabel, "0")
SetLabelText(ProtectedDirectoriesLabel, "0")
SetLabelText(SearchableDirectoriesLabel, "0")
SetLabelText(DirectoriesSearchedLabel, "0")
SetLabelText(FilesFoundLabel, "0")
Th1.Start()
SW = Stopwatch.StartNew()
StatisticsRefresher.Interval = 350
StatisticsRefresher.Enabled = True











End Sub



Private Sub RefreshButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RefreshButton.Click
RefreshButton.Text = "Refreshing Lists"
RefreshButton.Enabled = False
Th2.Start()
Th3.Start()
Th4.Start()
Th5.Start()
Th6.Start()
ListRefresher.Enabled = True
tmrCheckInfections.Enabled = True

End Sub


Private Sub StatusLabel_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles StatusLabel.TextChanged
If StatusLabel.Text = "Search Complete." Then
tmrRefreshList.Enabled = True

End If
End Sub

Private Sub tmrRefreshList_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrRefreshList.Tick
tmrRefreshList.Enabled = False
RefreshButton.Text = "Refreshing Lists"
RefreshButton.Enabled = False
Th2.Start()
Th3.Start()
Th4.Start()
Th5.Start()
ListRefresher.Enabled = True
tmrCheckInfections.Enabled = True
End Sub


Private Sub StatisticsRefresher_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StatisticsRefresher.Tick

End Sub

Private Sub ListRefresher_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListRefresher.Tick

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub


End Class


Any help will be appreciated, thanks .

Continue reading...
 
Back
Top