How to Get Recursive Listing of Files and Directories in VB.NET
Public Shared Function getRecursiveListing(ByVal initial As String) As List(Of String)
' This list stores the results.
Dim result As New List(Of String)
' This stack stores the directories to process.
Dim stack As New Stack(Of String)
' Add the initial directory
stack.Push(initial)
' Continue processing for each stacked directory
Do While (stack.Count > 0)
' Get top directory string
Dim dir As String = stack.Pop
Try
' Add all immediate file paths
result.AddRange(Directory.GetFiles(dir, "*.*"))
' Loop through all subdirectories and add them to the stack.
Dim directoryName As String
For Each directoryName In Directory.GetDirectories(dir)
stack.Push(directoryName)
Next
Catch ex As Exception
End Try
Loop
' Return the list
Return result
End Function
Dim list As List(Of String) = getRecursiveListing("C:\YourDirectoryPath")
' Loop through and display each path.
For Each path In list
Console.WriteLine(path)
Next
No comments:
Post a Comment