logo  VB.Net - List Files in Folder
This Chapter
Copy Delete ...
Is System File
Files in Folder
Special Folders
Chapters
Home Page
Colours, RGB
Computer Specifications
Dates&Times
Disk Drives
Files
Folders
GPS and OS Ref
VB.Net Forms
Image Files
If & Select
List/Array
Mathematics
NuGet
Sound
String Functions
Sun and Moon
User Controls
Validation
DigitalDan Sites
My Other Sites
Contact Site

Note
Some pages
may contain
inaccuracies
Hits=5
Get List of Files/Sub-Folders in a Folder
Unexpected errors (e.g. directory locked) can occur when looking at files or folders. I have assumed that you will provide your own Try...Catch logic to handle file access exceptions.
Get List of Files in Folder
This only returns files in main folder. It does not examine sub folders.
 

Public Function GetFilesInFolder(folder As String) As List(Of String)
 Dim ret As List(Of String) = IO.Directory.GetFiles(folder, "*.*", IO.SearchOption.TopDirectoryOnly).ToList
 Return ret
End Function
  
Get List of Files in Folder and Sub Folders
The following function return a list of all files within either a folder or it's sub folders. (i.e a Recursive search.) It will only work reliably when VB.Net is able to access all the files and sub folders. If any problem arises, it will throw an exception and return nothing. (Some files have access restrictions or can be locked by other porograms.) Use with caution because searching an entire hard disk could return millions of files and take a very long time. Consider placing this function in a backround worker whenebver it is likely to find too many fiels/sub-folders.

Public Function GetFilesInFolder(folder As String) As List(Of String)
 Dim ret As List(Of String) = IO.Directory.GetFiles(folder, "*.*", IO.SearchOption.AllDirectories).ToList
 Return ret
End Function
  

DigitalDan.co.uk