Why am I getting UnauthorizedAccessException on this folder?

↘锁芯ラ 提交于 2019-12-10 19:36:40

问题


According to MSDN, Directory.Exists should return false if a directory is not accessible. I have a path for which Directory.Exists returns true yet Directory.GetFiles throws a System.UnauthorizedAccessException. I have also tried the CanRead function here, but this too returns true for the path.

The path is "C:\Users\{username}\AppData\Local\Microsoft\Windows\INetCache\Content.IE5" if knowing that helps.


回答1:


You don't have access to content of this folder, because first - it's actually not a folder, but a reparse point which targets another folder and second - it have quite restrictive access rights.

In your particular case this reparse point targets "C:\Users\{username}\AppData\Local\Microsoft\Windows\INetCache\IE" folder which is freely accessible.

There are several such shortcuts exists inside user folder for compatibility with legacy software. And while you cannot list content of these reparse points, you can access files and folders inside when you know the name.

And last note, you never need to check specific folder rights before access, instead you should catch UnauthorizedAccessExpception and act accordingly. You don't even need to check folder existence before access, because it can be deleted after check (not this particular folder, but in general), you simply should catch DirectoryNotFoundException.



来源:https://stackoverflow.com/questions/46712285/why-am-i-getting-unauthorizedaccessexception-on-this-folder

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!