GetFiles from Directory and all subdirectories
Hi,
I am trying to write a small program in VB.NET which needs to get all the filenames (paths) in all subdirectories.
I want just be able to specify a top level directory e.g. "c:\images" - which may have several subdirectories and go down to a number levels with subdirectories within them.
Here's what I have done so far but this just gets the filenames and directories in the c:\images but doesn't get filename from directories within here.
imports system.io
dim folder as filesysteminfo
dim files() as string
files = folder.getfilesystementries("c:\images")
Any help will be very much appreciated. Many thanks
[638 byte] By [
theFaze] at [2008-2-2]
Hi,
You can use the following two methods below to extract all the File objects from directories based on a path specified.
In your code call ProcessAllDirectories by passing a Path and a blank arraylist object and the code will recursively get all sub directories and files within those as well.
Regards,
Vikram
| | Public Sub ProcessAllDirectories(ByVal strPath As String, ByRef objArrayList As ArrayList) Dim objException As Exception Try Dim objDirectoryInfo As New DirectoryInfo(strPath) ProcessDirectory(objDirectoryInfo, objArrayList) Catch objException Throw (objException) End Try End Sub Public Sub ProcessDirectory(ByVal objDirectoryInfo As DirectoryInfo, ByRef objArrayList As ArrayList) Try If objDirectoryInfo.GetFiles().Length > 0 Then Dim objFiles As FileInfo() Dim objFileInfo As FileInfo objFiles = objDirectoryInfo.GetFiles() For Each objFileInfo In objFiles objArrayList.Add(objFileInfo) Next End If If objDirectoryInfo.GetDirectories().Length > 0 Then Dim arrobjDirectoryInfo As DirectoryInfo() arrobjDirectoryInfo = objDirectoryInfo.GetDirectories() Dim objChildDirectory As DirectoryInfo For Each objChildDirectory In arrobjDirectoryInfo ProcessDirectory(objChildDirectory, objArrayList) Next End If Catch objException As Exception Throw (objException) End Try End Sub End Class |