PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Visual Studio 2008/VBListFilesInDirectory/FileEnumerator.vb

#
Visual Basic | 229 lines | 135 code | 58 blank | 36 comment | 0 complexity | 9479c8e25980faeb914ec33dfac19ca7 MD5 | raw file
  1. '/************************************* Module Header **************************************\
  2. '* Module Name: FileEnumerator.vb
  3. '* Project: VBListFilesInDirectory
  4. '* Copyright (c) Microsoft Corporation.
  5. '*
  6. '* The VBListFilesInDirectory project demonstrates how to implement an IEnumerable(Of String)
  7. '* that utilizes the Win32 File Management functions to enable application to get files and
  8. '* sub-directories in a specified directory one item a time.
  9. '*
  10. '* This source is subject to the Microsoft Public License.
  11. '* See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
  12. '* All other rights reserved.
  13. '*
  14. '* History:
  15. '* * 30/8/2009 1:00 PM Jie Wang Created
  16. '\******************************************************************************************/
  17. #Region "Using directives"
  18. Imports System
  19. Imports System.Collections
  20. Imports System.Collections.Generic
  21. Imports System.ComponentModel
  22. Imports System.Runtime.InteropServices
  23. #End Region
  24. Public Class DirectoryEnumerator
  25. Implements IEnumerable(Of String)
  26. #Region "The Enumerator"
  27. Public Structure Enumerator
  28. Implements IEnumerator(Of String)
  29. #Region "Private members"
  30. Private hFindFile As SafeFindHandle
  31. Private m_current As String
  32. Private m_pattern As String
  33. Private m_mode As Mode
  34. #End Region
  35. #Region "Constructor"
  36. Friend Sub New(ByVal pattern As String, ByVal mode As Mode)
  37. Me.m_pattern = pattern
  38. Me.m_current = Nothing
  39. Me.hFindFile = Nothing
  40. Me.m_mode = mode
  41. End Sub
  42. #End Region
  43. #Region "IEnumerator(Of String) Members"
  44. Public ReadOnly Property Current() As String Implements IEnumerator(Of String).Current
  45. Get
  46. Return Me.m_current
  47. End Get
  48. End Property
  49. #End Region
  50. #Region "IDisposable Members"
  51. Public Sub Dispose() Implements IDisposable.Dispose
  52. If Me.hFindFile IsNot Nothing Then
  53. Me.hFindFile.Dispose()
  54. End If
  55. End Sub
  56. #End Region
  57. #Region "IEnumerator Members"
  58. Public ReadOnly Property CurrentObject() As Object Implements IEnumerator.Current
  59. Get
  60. Return Me.m_current
  61. End Get
  62. End Property
  63. Public Function MoveNext() As Boolean Implements System.Collections.IEnumerator.MoveNext
  64. If Me.hFindFile Is Nothing Then
  65. Return FindFirst()
  66. Else
  67. Return FindNext()
  68. End If
  69. End Function
  70. Public Sub Reset() Implements System.Collections.IEnumerator.Reset
  71. If Me.hFindFile IsNot Nothing Then
  72. Me.hFindFile.Close()
  73. Me.hFindFile = Nothing
  74. End If
  75. End Sub
  76. #End Region
  77. #Region "Supporting Methods"
  78. ''' <summary>
  79. ''' Find the first match.
  80. ''' </summary>
  81. ''' <returns></returns>
  82. Private Function FindFirst() As Boolean
  83. Dim fd As New WIN32_FIND_DATA
  84. Me.hFindFile = NativeMethods.FindFirstFile(Me.m_pattern, fd)
  85. If Me.hFindFile.IsInvalid Then
  86. ' Got an invalid find handle, get the error code
  87. Dim code As Integer = Marshal.GetLastWin32Error()
  88. If code = NativeMethods.ERROR_FILE_NOT_FOUND Then
  89. ' file not found, just return false
  90. Return False
  91. End If
  92. ' other errors, throw exception
  93. Throw New Win32Exception(code)
  94. End If
  95. If Not AttributesMatchMode(fd.dwFileAttributes) Then
  96. ' if the file does not meet the match mode,
  97. ' go find the next match.
  98. Return FindNext()
  99. End If
  100. Me.m_current = fd.cFileName
  101. Return True
  102. End Function
  103. Private Function FindNext() As Boolean
  104. Dim fd As New WIN32_FIND_DATA
  105. While NativeMethods.FindNextFile(Me.hFindFile, fd)
  106. If Not AttributesMatchMode(fd.dwFileAttributes) Then
  107. ' if the file does not meet the match mode,
  108. ' go find the next match.
  109. Continue While
  110. End If
  111. ' found a match, return.
  112. Me.m_current = fd.cFileName
  113. Return True
  114. End While
  115. Dim code As Integer = Marshal.GetLastWin32Error()
  116. If code = NativeMethods.ERROR_NO_MORE_FILES Then
  117. ' no more files, return false.
  118. Return False
  119. End If
  120. ' other errors, throw exception.
  121. Throw New Win32Exception(code)
  122. End Function
  123. Private Function AttributesMatchMode(ByVal fileAttributes As Integer) As Boolean
  124. Dim isDir As Boolean = _
  125. ((fileAttributes And NativeMethods.FILE_ATTRIBUTE_DIRECTORY) = NativeMethods.FILE_ATTRIBUTE_DIRECTORY)
  126. Return (isDir AndAlso ((Me.m_mode And Mode.Directory) = Mode.Directory)) OrElse _
  127. (Not isDir AndAlso (Me.m_mode And Mode.File) = Mode.File)
  128. End Function
  129. #End Region
  130. End Structure
  131. #End Region
  132. #Region "FileEnumeratorMode"
  133. <Flags()> _
  134. Public Enum Mode
  135. ''' <summary>
  136. ''' Enumerate directories.
  137. ''' </summary>
  138. Directory = 1
  139. ''' <summary>
  140. ''' Enumerate files.
  141. ''' </summary>
  142. File = 2
  143. End Enum
  144. #End Region
  145. #Region "Private members"
  146. Private m_pattern As String ' Search pattern
  147. Private m_mode As Mode ' Enum mode
  148. #End Region
  149. #Region "Constructor"
  150. Public Sub New()
  151. Me.New("*.*")
  152. End Sub
  153. Public Sub New(ByVal pattern As String)
  154. Me.New(pattern, Mode.Directory Or Mode.File)
  155. End Sub
  156. Public Sub New(ByVal pattern As String, ByVal mode As Mode)
  157. Me.m_pattern = pattern
  158. Me.m_mode = mode
  159. End Sub
  160. #End Region
  161. #Region "IEnumerable(Of String) Members"
  162. Public Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of String) Implements IEnumerable(Of String).GetEnumerator
  163. Return New Enumerator(Me.m_pattern, Me.m_mode)
  164. End Function
  165. #End Region
  166. #Region "IEnumerable Memebers"
  167. Public Function GetEnumeratorObject() As System.Collections.IEnumerator Implements IEnumerable.GetEnumerator
  168. Return Me.GetEnumerator
  169. End Function
  170. #End Region
  171. End Class