tags:

views:

1739

answers:

3

I am writing some code to access directories and index the files it finds. The user is able to enter a UNC path and impersonate another user to get access to that directory. I am using Directory.Exists to see if the path they have entered is valid before trying to access it.

This works fine for local paths, but when trying to access a UNC path that I know I don't have access to, Directory.Exists is returning true.

My understanding is then that Directory.Exists does not check if the particular path can be "accessed" just that it exists or not.

What else can I use to check if a directory exists and I have read access to it ?

I don't know any of the file names inside the directory at this point.

Thanks!

+2  A: 

Why not simply attempt to perform your indexing by enumerating the files and handle the exception generated when its unable to access? There is always a possibility that a sub-folder or an individual file may not be accessible anyway even if you access the specific directory given.

AnthonyWJones
A: 

Interesting. I was thinking \directory\. would not be allowed though \directory should be OK, but \directory\. is OK as well, which I would consider a bug since in my opinion the '.' file is under the directory.

kenny
It makes sense to me that if you can see the directory, but not open it, that Directory.Exists would return true. Is that not what you are seeing?
chsh
@chsh, I agree but for me the '.' 'file' under the directory should not be visible since it is 'under'. Yes, I understand that the . points to itself.
kenny
A: 

Take a look at the System.Security.AccessControl.DirectorySecurity class.

joshperry