nieyangjie

转—VB.Net递归文件搜索实例

VB.Net递归文件搜索实例
代码如下:
  1. Option Strict On
  2. Option Explicit On
  3.  
  4. Imports System.IO
  5.  
  6.     Public Class FileSearch
  7.  
  8.         Private Const DefaultFileMask As String = "*.*"
  9.         Private Const DefaultDirectoryMask As String = "*"
  10.  
  11. #Region " Member Variables "
  12.  
  13.         Private _InitialDirectory As DirectoryInfo
  14.         Private _DirectoryMask As String
  15.         Private _FileMask As String
  16.  
  17.         '
  18.         Private _Directories As New ArrayList
  19.         Private _Files As New ArrayList
  20.  
  21. #End Region
  22.  
  23. #Region " Properites "
  24.  
  25.         Public Property InitialDirectory() As DirectoryInfo
  26.             Get
  27.                 Return _InitialDirectory
  28.             End Get
  29.             Set(ByVal Value As DirectoryInfo)
  30.                 _InitialDirectory = Value
  31.             End Set
  32.         End Property
  33.         Public Property DirectoryMask() As String
  34.             Get
  35.                 Return _DirectoryMask
  36.             End Get
  37.             Set(ByVal Value As String)
  38.                 _DirectoryMask = Value
  39.             End Set
  40.         End Property
  41.         Public Property FileMask() As String
  42.             Get
  43.                 Return _FileMask
  44.             End Get
  45.             Set(ByVal Value As String)
  46.                 _FileMask = Value
  47.             End Set
  48.         End Property
  49.  
  50.         Public ReadOnly Property Directories() As ArrayList
  51.             Get
  52.                 Return _Directories
  53.             End Get
  54.         End Property
  55.         Public ReadOnly Property Files() As ArrayList
  56.             Get
  57.                 Return _Files
  58.             End Get
  59.         End Property
  60.  
  61.  
  62.  
  63. #End Region
  64.  
  65. #Region " Constructors "
  66.  
  67.         Public Sub New()
  68.  
  69.         End Sub
  70.  
  71.         Public Sub New( _
  72.             ByVal BaseDirectory As String, _
  73.             Optional ByVal FileMask As String = DefaultFileMask, _
  74.             Optional ByVal DirectoryMask As String = DefaultDirectoryMask)
  75.  
  76.             Me.New(New IO.DirectoryInfo(BaseDirectory), FileMask, DirectoryMask)
  77.  
  78.         End Sub
  79.  
  80.         Public Sub New( _
  81.             ByVal BaseDirectory As DirectoryInfo, _
  82.             Optional ByVal FileMask As String = DefaultFileMask, _
  83.             Optional ByVal DirectoryMask As String = DefaultDirectoryMask)
  84.  
  85.             _InitialDirectory = BaseDirectory
  86.             _FileMask = FileMask
  87.             _DirectoryMask = DirectoryMask
  88.  
  89.         End Sub
  90.  
  91. #End Region
  92.  
  93.         Protected Overrides Sub Finalize()
  94.             _Files = Nothing
  95.             _Directories = Nothing
  96.             MyBase.Finalize()
  97.         End Sub
  98.  
  99.  
  100.         Public Sub Search( _
  101.             Optional ByVal BaseDirectory As DirectoryInfo = Nothing, _
  102.             Optional ByVal FileMask As String = Nothing, _
  103.             Optional ByVal DirectoryMask As String = Nothing)
  104.  
  105.             If Not IsNothing(BaseDirectory) Then
  106.                 _InitialDirectory = BaseDirectory
  107.             End If
  108.  
  109.             If IsNothing(_InitialDirectory) Then
  110.                 Throw New ArgumentException("A Directory Must be specified!", "Directory")
  111.             End If
  112.  
  113.             If IsNothing(FileMask) Then
  114.                 _FileMask = DefaultFileMask
  115.             Else
  116.                 _FileMask = FileMask
  117.             End If
  118.  
  119.  
  120.             If IsNothing(DirectoryMask) Then
  121.                 _DirectoryMask = DefaultDirectoryMask
  122.             Else
  123.                 _DirectoryMask = DirectoryMask
  124.             End If
  125.  
  126.             DoSearch(_InitialDirectory)
  127.         End Sub
  128.  
  129.         Private Sub DoSearch(ByVal BaseDirectory As DirectoryInfo)
  130.  
  131.             Try
  132.                 _Files.AddRange(BaseDirectory.GetFiles(_FileMask))
  133.             Catch u As UnauthorizedAccessException
  134.                 'Siliently Ignore this error, there isnt any simple
  135.                 'way to avoid this error.
  136.  
  137.             End Try
  138.  
  139.             Try
  140.                 Dim Directories() As DirectoryInfo = BaseDirectory.GetDirectories(_DirectoryMask)
  141.                 _Directories.AddRange(Directories)
  142.  
  143.                 For Each di As DirectoryInfo In Directories
  144.                     DoSearch(di)
  145.                 Next
  146.  
  147.             Catch u As UnauthorizedAccessException
  148.                 'Siliently Ignore this error, there isnt any simple
  149.                 'way to avoid this error.
  150.  
  151.             End Try
  152.  
  153.         End Sub
  154.  
  155.     End Class

调用方法

代码如下:
  1. Dim x As New FileSearch(New IO.DirectoryInfo("d:\Media\"), "*.nfo")
  2.  
  3.         x.Search()
  4.  
  5.         MessageBox.Show(x.Files.Count) ' number of files that match "*.nfo" in D:\Media
  6.         MessageBox.Show(x.Directories.Count) ' the total number of directories looked through
  7.  

posted on 2010-12-23 13:06  nieyangjie  阅读(...)  评论(...编辑  收藏

My Links

Blog Stats