A Windows service running as a system cannot access C: \ users \

I have a Windows service installed as a local system account, and sometimes it creates a list of directories on the machine. It does not work on Windows 7 in the c: \ users directory .... I checked these folders and they seem to be under full control of the system account. Why can't I 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 more details, but I also posted a class that I used to solve this problem. See GitHub Gist - DirectoryHelper

+4
source share
3 answers

So, I myself answered this. The problem is using Directory.GetFiles() or Directory.GetDirectories() . On Windows 7, using these methods, you will encounter junction points and / or hard links when searching under the C: \ Users ... tree due to junction points added for backward compatibility with XP generation applications. MS throws a UnauthorizedAccessException when you try to read one of these connections (to prevent possible endless loops in the location of these transitions), and causes the GetFiles() or GetDirectories() call to complete without returning anything. As a solution, I implemented the http://tom-shelton.net/index.php/2010/01/02/using-extension-methods-and-the-win32-api-to-efficiently-enumerate-the-file- option system / , which will iterate over the files one at a time, allowing you to handle the exception when you click the connection and continue. Pretty dumb question, I hope that it helps someone.

+1
source

As you laid out to my question with an idea, I thought that I would return this service. Yes, I know that you have found a solution already ...

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

This allows you to request privilege escalation when processing errors if you want to access unauthorized files or folders.

Hope this helps.

Greetings

Ben

+1
source

Stay tuned for more error information with tools like Process Monitor from Sysinternals (Microsoft Technet). The message may be misleading; Some general conditions for failed errors include open files and antiviruses.

0
source

Source: https://habr.com/ru/post/1394104/


All Articles