Advertisement
coderail

Fast/Safe FileSearch - VB.NET

Apr 24th, 2012
1,077
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. Imports System.IO
  2. Imports System.Threading
  3.  
  4. '------------------
  5. 'Creator: aeonhack
  6. 'Site: elitevs.net
  7. 'Created: 4/17/2011
  8. 'Changed: 4/24/2012
  9. 'Version: 1.1.0
  10. '------------------
  11.  
  12. Class FileSearch
  13.  
  14. Private Callback As CallbackCB
  15. Public Delegate Sub CallbackCB()
  16.  
  17. Sub New(_callback As CallbackCB)
  18. Callback = _callback
  19. _Files = New List(Of String)
  20. End Sub
  21.  
  22. Private _Files As List(Of String)
  23. ReadOnly Property Files As String()
  24. Get
  25. SyncLock _Files
  26. Return _Files.ToArray()
  27. End SyncLock
  28. End Get
  29. End Property
  30.  
  31. Private _Pattern As String
  32. ReadOnly Property Pattern As String
  33. Get
  34. Return _Pattern
  35. End Get
  36. End Property
  37.  
  38. Private _Searching As Boolean
  39. ReadOnly Property Searching As Boolean
  40. Get
  41. Return _Searching
  42. End Get
  43. End Property
  44.  
  45. Private _ShowHiddenFiles As Boolean = True
  46. Property ShowHiddenFiles() As Boolean
  47. Get
  48. Return _ShowHiddenFiles
  49. End Get
  50. Set(ByVal value As Boolean)
  51. _ShowHiddenFiles = value
  52. End Set
  53. End Property
  54.  
  55. Public Sub Search(path As String, pattern As String)
  56. If _Searching Then Return
  57.  
  58. Track = 0
  59. _Searching = True
  60. _Pattern = pattern
  61. _Files = New List(Of String)
  62. _Cancel = False
  63.  
  64. Dim T As New Thread(AddressOf Initialize)
  65. T.IsBackground = True
  66. T.Start(path)
  67. End Sub
  68.  
  69. Private Sub Initialize(data As Object)
  70. Process(DirectCast(data, String))
  71. End Sub
  72.  
  73. Private Track As Integer
  74. Private Sub Search(data As Object)
  75. If _Cancel Then Return
  76. Dim Path As String = DirectCast(data, String)
  77.  
  78. Try
  79. For Each P As String In Directory.GetDirectories(Path)
  80. If _ShowHiddenFiles Then
  81. Process(P)
  82. ElseIf Not IsHidden((New DirectoryInfo(P)).Attributes) Then
  83. Process(P)
  84. End If
  85. Next
  86. Catch
  87. 'Do nothing
  88. End Try
  89.  
  90. Try
  91. SyncLock _Files
  92. If _ShowHiddenFiles Then
  93. _Files.AddRange(Directory.GetFiles(Path, _Pattern))
  94. Else
  95. For Each F As String In Directory.GetFiles(Path, _Pattern)
  96. If Not IsHidden(File.GetAttributes(F)) Then _Files.Add(F)
  97. Next
  98. End If
  99. End SyncLock
  100. Catch
  101. 'Do nothing.
  102. End Try
  103.  
  104. SyncLock Me
  105. Track -= 1
  106. If Track = 0 Then
  107. _Searching = False
  108. If Callback IsNot Nothing Then Callback()
  109. End If
  110. End SyncLock
  111. End Sub
  112.  
  113. Private Sub Process(path As String)
  114. SyncLock Me
  115. Track += 1
  116. End SyncLock
  117.  
  118. If Not ThreadPool.QueueUserWorkItem(AddressOf Search, path) Then Search(path)
  119. End Sub
  120.  
  121. Private _Cancel As Boolean
  122. Public Sub Cancel()
  123. _Cancel = True
  124. _Searching = False
  125. End Sub
  126.  
  127. Public Sub Clear()
  128. _Files.Clear()
  129. End Sub
  130.  
  131. Private Function IsHidden(a As FileAttributes) As Boolean
  132. If ((a And FileAttributes.Hidden) = FileAttributes.Hidden) Then Return True
  133. If ((a And FileAttributes.System) = FileAttributes.System) Then Return True
  134. Return False
  135. End Function
  136.  
  137. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement