0

i need to explore different folders and i try to prevent errors instead to do a try/catch on each folder . I had an error on the 'C:\Windows\InfusedApps' folder when trying to list files.

UnauthorizedAccessException:'System.UnauthorizedAccessException: Access to the path 'C:\Windows\InfusedApps' is denied.

i would to know if there were a way to test, i tried different things but without success. By example DirectoryInfo has just the "directory" flag like a normal directory.

I tried to use ` DirectorySecurity test = FileSystemAclExtensions.GetAccessControl ( new DirectoryInfo(parent.Path)

` But i don't see how use it to detect a difference.

Thanks.

Vonkel.
  • 306
  • 3
  • 12
  • 1
    @KenWhite: I think that's not the right duplicate. I'm sure this is a duplicate, but I think the OP isn't asking how to change the failure into a success, but rather how to detect the case that will fail before the exception is thrown. – Peter Duniho Jan 05 '21 at 21:51
  • 1
    It is theoretically possible to inspect the ACL for the files and directories before the fact and to only attempt to discover those for which you've determined the current user has rights. But IMHO that's incredibly impractical. The exception provides all the information you need, and it's simple enough to just handle it. See first duplicate. If on the other hand you want for your program to actually be able to deal with files and directories it currently cannot, see second duplicate for information on adjusting your user privileges. – Peter Duniho Jan 05 '21 at 21:55
  • It's right i wanted to prevent, make a try catch have a cost the question for me it's to know if it's more interesting to prevent the commons. Furthermore the answer is based on a os that have 11years and same for the environment. With .net core 3 there some api changed then i asked if things have changed. With ACL i didn't find examples. On another side perhaps prevent cost more than try/catch, i will search deeper. Thanks for your answers and support. – Vonkel. Jan 06 '21 at 07:21

0 Answers0