PageRenderTime 12ms CodeModel.GetById 1ms app.highlight 9ms RepoModel.GetById 1ms 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
 18
 19#Region "Using directives"
 20Imports System
 21Imports System.Collections
 22Imports System.Collections.Generic
 23Imports System.ComponentModel
 24Imports System.Runtime.InteropServices
 25#End Region
 26
 27
 28Public Class DirectoryEnumerator
 29    Implements IEnumerable(Of String)
 30
 31#Region "The Enumerator"
 32    Public Structure Enumerator
 33        Implements IEnumerator(Of String)
 34
 35#Region "Private members"
 36
 37        Private hFindFile As SafeFindHandle
 38        Private m_current As String
 39        Private m_pattern As String
 40        Private m_mode As Mode
 41
 42#End Region
 43
 44#Region "Constructor"
 45
 46        Friend Sub New(ByVal pattern As String, ByVal mode As Mode)
 47            Me.m_pattern = pattern
 48            Me.m_current = Nothing
 49            Me.hFindFile = Nothing
 50            Me.m_mode = mode
 51        End Sub
 52
 53#End Region
 54
 55#Region "IEnumerator(Of String) Members"
 56
 57        Public ReadOnly Property Current() As String Implements IEnumerator(Of String).Current
 58            Get
 59                Return Me.m_current
 60            End Get
 61        End Property
 62
 63#End Region
 64
 65#Region "IDisposable Members"
 66
 67        Public Sub Dispose() Implements IDisposable.Dispose
 68            If Me.hFindFile IsNot Nothing Then
 69                Me.hFindFile.Dispose()
 70            End If
 71        End Sub
 72
 73#End Region
 74
 75#Region "IEnumerator Members"
 76
 77        Public ReadOnly Property CurrentObject() As Object Implements IEnumerator.Current
 78            Get
 79                Return Me.m_current
 80            End Get
 81        End Property
 82
 83        Public Function MoveNext() As Boolean Implements System.Collections.IEnumerator.MoveNext
 84            If Me.hFindFile Is Nothing Then
 85                Return FindFirst()
 86            Else
 87                Return FindNext()
 88            End If
 89        End Function
 90
 91        Public Sub Reset() Implements System.Collections.IEnumerator.Reset
 92            If Me.hFindFile IsNot Nothing Then
 93                Me.hFindFile.Close()
 94                Me.hFindFile = Nothing
 95            End If
 96        End Sub
 97
 98#End Region
 99
100#Region "Supporting Methods"
101
102        ''' <summary>
103        ''' Find the first match.
104        ''' </summary>
105        ''' <returns></returns>
106        Private Function FindFirst() As Boolean
107            Dim fd As New WIN32_FIND_DATA
108
109            Me.hFindFile = NativeMethods.FindFirstFile(Me.m_pattern, fd)
110
111            If Me.hFindFile.IsInvalid Then
112                ' Got an invalid find handle, get the error code
113                Dim code As Integer = Marshal.GetLastWin32Error()
114
115                If code = NativeMethods.ERROR_FILE_NOT_FOUND Then
116                    ' file not found, just return false
117                    Return False
118                End If
119
120                ' other errors, throw exception
121                Throw New Win32Exception(code)
122            End If
123
124            If Not AttributesMatchMode(fd.dwFileAttributes) Then
125                ' if the file does not meet the match mode,
126                ' go find the next match.
127                Return FindNext()
128            End If
129
130            Me.m_current = fd.cFileName
131            Return True
132        End Function
133
134        Private Function FindNext() As Boolean
135            Dim fd As New WIN32_FIND_DATA
136
137            While NativeMethods.FindNextFile(Me.hFindFile, fd)
138                If Not AttributesMatchMode(fd.dwFileAttributes) Then
139                    ' if the file does not meet the match mode,
140                    ' go find the next match.
141                    Continue While
142                End If
143
144                ' found a match, return.
145                Me.m_current = fd.cFileName
146                Return True
147            End While
148
149            Dim code As Integer = Marshal.GetLastWin32Error()
150
151            If code = NativeMethods.ERROR_NO_MORE_FILES Then
152                ' no more files, return false.
153                Return False
154            End If
155
156            ' other errors, throw exception.
157            Throw New Win32Exception(code)
158        End Function
159
160        Private Function AttributesMatchMode(ByVal fileAttributes As Integer) As Boolean
161            Dim isDir As Boolean = _
162                ((fileAttributes And NativeMethods.FILE_ATTRIBUTE_DIRECTORY) = NativeMethods.FILE_ATTRIBUTE_DIRECTORY)
163
164            Return (isDir AndAlso ((Me.m_mode And Mode.Directory) = Mode.Directory)) OrElse _
165                    (Not isDir AndAlso (Me.m_mode And Mode.File) = Mode.File)
166        End Function
167
168#End Region
169
170    End Structure
171#End Region
172
173#Region "FileEnumeratorMode"
174
175    <Flags()> _
176    Public Enum Mode
177        ''' <summary>
178        ''' Enumerate directories.
179        ''' </summary>
180        Directory = 1
181        ''' <summary>
182        ''' Enumerate files. 
183        ''' </summary>
184        File = 2
185    End Enum
186
187#End Region
188
189#Region "Private members"
190
191    Private m_pattern As String     ' Search pattern
192    Private m_mode As Mode          ' Enum mode
193
194#End Region
195
196#Region "Constructor"
197
198    Public Sub New()
199        Me.New("*.*")
200    End Sub
201
202    Public Sub New(ByVal pattern As String)
203        Me.New(pattern, Mode.Directory Or Mode.File)
204    End Sub
205
206    Public Sub New(ByVal pattern As String, ByVal mode As Mode)
207        Me.m_pattern = pattern
208        Me.m_mode = mode
209    End Sub
210
211#End Region
212
213#Region "IEnumerable(Of String) Members"
214
215    Public Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of String) Implements IEnumerable(Of String).GetEnumerator
216        Return New Enumerator(Me.m_pattern, Me.m_mode)
217    End Function
218
219#End Region
220
221#Region "IEnumerable Memebers"
222
223    Public Function GetEnumeratorObject() As System.Collections.IEnumerator Implements IEnumerable.GetEnumerator
224        Return Me.GetEnumerator
225    End Function
226
227#End Region
228
229End Class