Dir listing

DavyEFC

Well-known member
Joined
Dec 17, 2010
Messages
51
Programming Experience
5-10
I have come across an issue that was previously (embarrassingly) unthought of by me, and am not having much luck searching for a suitable solution.

I can use

VB.NET:
ArrDirs = Directory.GetDirectories(Pth, "*", 1)

to get an array of directories for a given path. I foolishly thought this would return a list of dirs whether i had access or not (as i'm not actually accessing them yet, just seeing what's there!) But if i set my path to, say, F drive (which is the root of my external hard drive) the command fails on dirs that i don't have access to such as Recycler and System Volume Info.
What is the train of thought to do this correctly, i.e. return an array of all dirs in the path while skipping errors?
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,602
Location
Sydney, Australia
Programming Experience
10+
Firstly, the third parameter of that GetDirectories method is a SearchOption value, not an Integer, so you should be using a SearchOption value. You must have Option Strict Off for that to compile so I suggest that you turn it On for this project and every other project and use the correct types at all times.

As for the question, with regards to the errors, I think you'll find that the exception occurs not when you get the path of an inaccessible folder but when you try to get its contents, which makes perfect sense. The solution is to write your own recursive method that gets one level of folders on each call. You can then catch an UnauthorizedAccessException and just ignore that folder and move on. You'll find numerous examples of a recursive file/folder search online.
 
Top Bottom