Answered by:

Getting over the dreaded "Access Denied" error with Directory.GetFiles

Question

I've been researching this topic quite a bit and cannot find an answer.

I am writing a utility where one of its function is to scan for certain file types. Naturally, I use the Directory.GetFiles function. I have code to ignore folders and/or files I don't have access to. So that isn't the issue. My issue is, I
want to access those files.

The utility runs as a C# console application. I've included a custom manifest file that requires Administrator privileges to run. When I test this on my machine, my function won't even access Doc Settings/My Local Admin User/My Documents & My Pictures...etc.How
is this possible?! If I am an Administrator on this machine, running a console app with administrator privileges, why can't I even access this folder? I even logged in as the main Administrator account and still can't get access.

I am running on Windows 7. Developing in VS 2010. .NET Framework 4.0.

The question: How do I gain access to the folders/files that give me an UnauthorizedAccessException? I know there is a way around this.

Answers

If you go to the parent directories (say for example %userprofile%) in cmd and executed the dir command, is "My Documents" listed? I doubt it. "Documents" is there by default but "My Documents" is not. The list of directories you listed out earlier as
being the ones that cause problems are directories which are actually just redirections to other directories. So your program is going to find the real directory, meaning you can safely ignore the empty Reparse point ones.

All replies

I added a custom manifest to the project and have it embedded. I know it runs as Administrator because when I run it from my the command window it asks for consent to run as Administrator. Is that what you mean?

I'm just trying to do a search for certain file types. These are places I can get to normally under any and all other circumstances except when I run from my console application and use the Directory.GetFiles

Access to the path 'C:\Documents and Settings\' is denied.
Access to the path 'C:\ProgramData\Application Data\' is denied.
Access to the path 'C:\ProgramData\Desktop\' is denied.
Access to the path 'C:\ProgramData\Documents\' is denied.
Access to the path 'C:\ProgramData\Favorites\' is denied.
Access to the path 'C:\ProgramData\Start Menu\' is denied.
Access to the path 'C:\ProgramData\Templates\' is denied.
Access to the path 'C:\System Volume Information\' is denied.
Access to the path 'C:\Users\AdminUser\AppData\Local\Application Data\' is denied.
Access to the path 'C:\Users\AdminUser\AppData\Local\History\' is denied.
Access to the path 'C:\Users\AdminUser\AppData\Local\Temporary Internet Files\' is denied.
Access to the path 'C:\Users\AdminUser\Application Data\' is denied.
Access to the path 'C:\Users\AdminUser\Cookies\' is denied.
Access to the path 'C:\Users\AdminUser\Documents\My Music\' is denied.
Access to the path 'C:\Users\AdminUser\Documents\My Pictures\' is denied.
Access to the path 'C:\Users\AdminUser\Documents\My Videos\' is denied.
Access to the path 'C:\Users\AdminUser\Local Settings\' is denied.
Access to the path 'C:\Users\AdminUser\My Documents\' is denied.
Access to the path 'C:\Users\AdminUser\NetHood\' is denied.
Access to the path 'C:\Users\AdminUser\PrintHood\' is denied.
Access to the path 'C:\Users\AdminUser\Recent\' is denied.
Access to the path 'C:\Users\AdminUser\SendTo\' is denied.
Access to the path 'C:\Users\AdminUser\Start Menu\' is denied.
Access to the path 'C:\Users\AdminUser\Templates\' is denied.
Access to the path 'C:\Users\All Users\Application Data\' is denied.
Access to the path 'C:\Users\All Users\Desktop\' is denied.
Access to the path 'C:\Users\All Users\Documents\' is denied.
Access to the path 'C:\Users\All Users\Favorites\' is denied.
Access to the path 'C:\Users\All Users\Start Menu\' is denied.
Access to the path 'C:\Users\All Users\Templates\' is denied.
Access to the path 'C:\Users\Default\AppData\Local\Application Data\' is denied.
Access to the path 'C:\Users\Default\AppData\Local\History\' is denied.
Access to the path 'C:\Users\Default\AppData\Local\Temporary Internet Files\' is denied.
Access to the path 'C:\Users\Default\Application Data\' is denied.
Access to the path 'C:\Users\Default\Cookies\' is denied.
Access to the path 'C:\Users\Default\Documents\My Music\' is denied.
Access to the path 'C:\Users\Default\Documents\My Pictures\' is denied.
Access to the path 'C:\Users\Default\Documents\My Videos\' is denied.
Access to the path 'C:\Users\Default\Local Settings\' is denied.
Access to the path 'C:\Users\Default\My Documents\' is denied.
Access to the path 'C:\Users\Default\NetHood\' is denied.
Access to the path 'C:\Users\Default\PrintHood\' is denied.
Access to the path 'C:\Users\Default\Recent\' is denied.
Access to the path 'C:\Users\Default\SendTo\' is denied.
Access to the path 'C:\Users\Default\Start Menu\' is denied.
Access to the path 'C:\Users\Default\Templates\' is denied.
Access to the path 'C:\Users\Default User\' is denied.
Access to the path 'C:\Users\My Other User\AppData\Local\Application Data\' is denied.
Access to the path 'C:\Users\My Other User\AppData\Local\History\' is denied.
Access to the path 'C:\Users\My Other User\AppData\Local\Temporary Internet Files\' is denied.
Access to the path 'C:\Users\My Other User\Application Data\' is denied.
Access to the path 'C:\Users\My Other User\Cookies\' is denied.
Access to the path 'C:\Users\My Other User\Documents\My Music\' is denied.
Access to the path 'C:\Users\My Other User\Documents\My Pictures\' is denied.
Access to the path 'C:\Users\My Other User\Documents\My Videos\' is denied.
Access to the path 'C:\Users\My Other User\Local Settings\' is denied.
Access to the path 'C:\Users\My Other User\My Documents\' is denied.
Access to the path 'C:\Users\My Other User\NetHood\' is denied.
Access to the path 'C:\Users\My Other User\PrintHood\' is denied.
Access to the path 'C:\Users\My Other User\Recent\' is denied.
Access to the path 'C:\Users\My Other User\SendTo\' is denied.
Access to the path 'C:\Users\My Other User\Start Menu\' is denied.
Access to the path 'C:\Users\My Other User\Templates\' is denied.
Access to the path 'C:\Users\Public\Documents\My Music\' is denied.
Access to the path 'C:\Users\Public\Documents\My Pictures\' is denied.
Access to the path 'C:\Users\Public\Documents\My Videos\' is denied.
Access to the path 'C:\Windows\CSC\v2.0.6\' is denied.
Access to the path 'C:\Windows\System32\LogFiles\WMI\RtBackup\' is denied.

What's the value you're using for directoryName? And what's the value for pattern? I see you're specifying TopDirectoryOnly and yet the output is showing child directories.

I am starting at C:\ that is directory name. The code I put is inside an iterative function. So I eventually make it down the whole Directory tree. Pattern can be anything. Right now, I'm targeting *.pf and *svch*.* . I can get to all the other directories
just fine.

For starters, the following paths for every user are not actual directories, and therefore there is no need for you to access them (if that is even possible)

\Documents\My Music\

\Documents\My Pictures\

\Documents\My Videos\

\My Documents\

The actual directories for the above are

\My Music\

\My Pictures\

\My Videos\

\Documents\

This most likely applies to some of the other directories you have listed.

Also, note that while explorer shows the documents folder as "My Documents", if you click in explorer's address bar to change the path, it shows that it actually is called "Documents".

Cool. How can I get to them programmatically? What about the other paths? Also, if those aren't the actual paths, then wouldn't I get a different exception - PathDoesNotExist(?) vs. UnauthorizedAccessException ?

I am inside an iterative function. I will generally start my search at C:\ and make it all the way down. So when I get to these directories, how do I know? I use Directory.GetDirectories(directoryName) to get the child directories.

You probably are going to those directories (C:\users\Public\Documents), you're just not getting an error for them. Most of those directories you're listing look like directories from Win XP, not Win 7.

It is odd that you're getting an accessed denied instead of a DirectoryNotFound.

You probably are going to those directories (C:\users\Public\Documents), you're just not getting an error for them. Most of those directories you're listing look like directories from Win XP, not Win 7.

It is odd that you're getting an accessed denied instead of a DirectoryNotFound.

So should I be using a different method to get sub directories? Or is there a function that finds the correct path mapping?

Doing a little poking around I see that those directories have the Hidden and ReparsePoint attributes. According to
http://msdn.microsoft.com/en-us/library/aa365503(VS.85).aspx "Reparse points can be established for a directory, but the directory must be empty." So you could probably ignore directories which are ReparsePoints.

Doing a little poking around I see that those directories have the Hidden and ReparsePoint attributes. According to
http://msdn.microsoft.com/en-us/library/aa365503(VS.85).aspx "Reparse points can be established for a directory, but the directory must be empty." So you could probably ignore directories which are ReparsePoints.

Understood. But I know for a fact that a bunch of those directories on my local machine are not empty. Good to know though.

If you go to the parent directories (say for example %userprofile%) in cmd and executed the dir command, is "My Documents" listed? I doubt it. "Documents" is there by default but "My Documents" is not. The list of directories you listed out earlier as
being the ones that cause problems are directories which are actually just redirections to other directories. So your program is going to find the real directory, meaning you can safely ignore the empty Reparse point ones.

I still have this problem... Perhaps a post of some working code might clear up some of the ambiguity. (Instead of a thousand people all throwing out a bunch of suggestions, and they i try each one of them and finally idea number 998 works)... and let
me be the first to say that this is a terrible issue and I just wrote my own getDirectories method and my own getFiles method to get around it... which leaves me with like thirty or forty lines of code as opposed to one...

I am running Visual Studio 2010 as an administrator, and I am getting a System.UnauthorizedAccess error (what higher privilege do i need?!?).... ( Access to the path 'C:\Users\John.Bartels\Documents\My Videos' is denied.)... That directory does not
even exist (be it from explorer, cmd, or powershell that I view the directory structure).

Can someone please post a few Working lines of Code that circumvent this error and adequately explain the issue. I know that the "AllDirectories" search option includes reparse points like mounted drives and symbolic links. So... why wouldn't
I have access to a symbolic link directory when I am running as administrator... Still makes no sense. Please Post Code.

I would like to Specify "AllDirectories" as my search option (because I don't want to my own recursive methods... why not?... because if the AllDirectories search option is there and i *Should be able to search a directory tree in one line of code... then
that's what I would like to do.

//should have just went to stackoverflow for my answer in the first place... simple explanation

I am getting a System.UnauthorizedAccess error (what higher privilege do i need?!?).... ( Access to the path 'C:\Users\John.Bartels\Documents\My Videos' is denied.)... That directory does not even exist (be it from explorer, cmd, or powershell that
I view the directory structure).

Otherwise, the call to GetDirectories with the SearchOption set to AllDirectories... will almost certainly fail... so why would I ever want to use it???... seems like there should be a boolean flag here that can leave out the Reparse Points... maybe then
this Method Call would actually have some utility.