Hi.
I have a function that returns a value of the numbers of files, folders and subfolders in a directory. But i can't seem to handle the "UnauthorizedAccessException" which i get from certain read only files or folders. I tried to use both the DirectoryInfo.GetDirectories() and DirectoryInfo.GetFiles() methods. as you can see from the code below. This exception is thrown in start of the for each loop, so i'm not able to handel it with a try, Catch Ex As UnauthorizedAccessException to cintinue the loop.
Code Code:
Function CountFolders(ByVal Directory As String) As Integer Dim FolderCount As Integer = 0 Dim SubDirectories As New ArrayList Dim FolderInfo As DirectoryInfo = New DirectoryInfo(Directory) For Each Dir As DirectoryInfo In FolderInfo.GetDirectories If (Dir.Attributes And FileAttribute.System) = FileAttributes.System Then Continue For End If SubDirectories.Add(Dir.FullName) FolderCount += 1 Next For Each item As String In SubDirectories FolderCount = CountFolders(item) + FolderCount Next Return FolderCount End Function
How can i handle this exception and continue the loop.




Reply With Quote