VBCoding Библиотека кодов Visual Basic .NET Файловая система Найти файл

Visual Basic .NET
Найти файл
Импортируйте System.IO.

Класс:
Public Class FileSearcher

Private _Matches As New ArrayList()
Private _FileFilter As String
Private _Recursive As Boolean

Public ReadOnly Property Matches() As ArrayList
Get
Return _Matches
End Get
End Property

Public Property FileFilter() As String
Get
Return _FileFilter
End Get
Set(ByVal Value As String)
_FileFilter = Value
End Set
End Property

Public Sub New(ByVal fileFilter As String)
Me.FileFilter = fileFilter
End Sub

Public Sub Search(ByVal startingPath As String, _
ByVal recursive As Boolean)
Matches.Clear()
Me._Recursive = recursive
SearchDirectory(New DirectoryInfo(startingPath))
End Sub

Private Sub SearchDirectory(ByVal dir As DirectoryInfo)
Dim FileItem As FileInfo
For Each FileItem In dir.GetFiles(Me.FileFilter)
_Matches.Add(FileItem)
Next
If Me._Recursive Then
Dim DirItem As DirectoryInfo
For Each DirItem In dir.GetDirectories()
Try
SearchDirectory(DirItem)
Catch Err As UnauthorizedAccessException
End Try
Next
End If

End Sub

End Class

Использование:
Dim Searcher As New FileSearcher("*.txt") 'Маска поиска
'Если хотите, что бы поиск велся и в подкаталогах, поставьте замените False на True
Searcher.Search("c:\Windows\", False)
Dim File As FileInfo, r As String
For Each File In Searcher.Matches
r = r & File.FullName
Next
MsgBox r
 

Добавить комментарий


Защитный код
Обновить

 
VBCoding Библиотека кодов Visual Basic .NET Файловая система Найти файл  
Powered by Exponenta -