4

I've got a Windows service installed as the local system account, and occasionally it builds a list of directories on the machine. It fails on Windows 7 under the c:\users... directories. I checked those folders out, and they appear to be under Full Control to the system account. Why would I be unable to access these directories?

System.UnauthorizedAccessException: Access to the path 'C:\Users\Public\Documents\My         Videos' is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.Directory.InternalGetFileDirectoryNames(String path, String userPathOriginal,   String searchPattern, Boolean includeFiles, Boolean includeDirs, SearchOption searchOption)
at System.IO.Directory.GetDirectories(String path, String searchPattern, SearchOption searchOption)
at LS.Core.Backup.DirectoryMapper.GetDirectories(String SeedDir, Int32 Count)

Update:

See my answer below for details, but I've also posted the class I used to work around this issue. See GitHub Gist - DirectoryHelper

Grant H.
  • 3,689
  • 2
  • 35
  • 53
  • Is there an `InnerException`? Please post the complete exception trace. Also, what function are you calling? What privileges does your program have? – cyanic Feb 01 '12 at 02:25
  • I've added the stack-trace, don't have an inner exception unfortunately. I'm running as the SYSTEM account, so shouldn't that give me access to the entire drive? – Grant H. Feb 01 '12 at 02:29
  • If the exception includes an `HResult` field, have a look at what it is. From what I can tell by the MSDN documentation (http://msdn.microsoft.com/en-us/library/system.exception.hresult.aspx), it should be the same HRESULT that the native Windows call will return. Also as Grant H. said, try Process Monitor. In case `HResult` is not set, you can see the code from Process Monitor. – cyanic Feb 01 '12 at 02:42

3 Answers3

1

As you posted on mine with an idea, I thought I would return the favour. Yes I know you found a solution already....

I used a variation of this iterative search from MSDN - Iterate Through a Directory Tree

This allows you in your error handling to prompt an elevation in privileges if you so wish to gain access to unauthorised files or folders.

Hope it helps.

Cheers,

Ben

1

So I ended up answering this myself. The issue is with using Directory.GetFiles() or Directory.GetDirectories(). In Windows 7, using these methods, you will encounter junction points and/or hard links when searching beneath the C:\Users... tree because of the junction points added in for backwards compatibility with XP generation apps. MS fires an UnauthorizedAccessException when you try and read one of these junctions (to prevent possible infinite loops in the arrangement of those junctions), and it causes the entire GetFiles() or GetDirectories() call to fail, returning nothing. As a solution, I implemented a variant of http://tom-shelton.net/index.php/2010/01/02/using-extension-methods-and-the-win32-api-to-efficiently-enumerate-the-file-system/, which will iterate files one at a time, allowing you to handle the exception when you hit a junction and continue on. Pretty obtuse issue, hope it helps someone.

Grant H.
  • 3,689
  • 2
  • 35
  • 53
0

Watch for more specific error details with tools such as Process Monitor from Sysinternals (Microsoft Technet). The message may be misleading; some common conditions for "access denied" errors include open files and antiviruses.

Humberto
  • 7,117
  • 4
  • 31
  • 46
  • Thanks, but this machine does not have anti virus, and it does this for any and all directories under the c:\users\ branch, so it seems like some form of security issue. – Grant H. Feb 01 '12 at 02:25