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