C# – Windows service running as system cannot access C:\users\

c

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

Best Answer

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

Related Topic