I'm creating a C# program which check signatures for all .exe files and do some other checks. The application request to run as admin with UAC, and checks it at start (We are sure is running with admin privileges).
However, when I check recursively some paths, I got folders where I don't have access. For example:
DirectoryInfo dir = new DirectoryInfo(System.Environment.GetFolderPath(System.Environment.SpecialFolder.CommonApplicationData));
FileInfo[] files = dir.GetFiles("*.exe", SearchOption.AllDirectories);
is throwing:
System.UnauthorizedAccessException: Access to the path 'C:\ProgramData\Application Data' is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileSystemEnumerableIterator`1.AddSearchableDirsToStack(SearchData localSearchData)
at System.IO.FileSystemEnumerableIterator`1.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.IO.DirectoryInfo.InternalGetFiles(String searchPattern, SearchOption searchOption)
at "the lines above"
Is there any way to access those paths? In case it's not possible, a way to GetFiles skips those paths without stopping is also very appreciated.
Just because you are an admin does not mean you can access everything!
E.g. a users personal folder is also not accessible for an admin "by default", but as an admin you can "give yourself that right".
So either you change the rights on all such folders or just leave out such folders.
UPDATE:
You could choose a recursive scan "per folder" (should be done anyhow IMO):