为什么我在此文件夹上收到 UnauthorizedAccessException?

Why am I getting UnauthorizedAccessException on this folder?

根据 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,但这对于路径也是如此 returns。

路径是 "C:\Users\{username}\AppData\Local\Microsoft\Windows\INetCache\Content.IE5" 如果知道有帮助的话。

您无权访问此文件夹的内容,因为首先 - 它实际上不是一个文件夹,而是一个 reparse point 以另一个文件夹为目标,其次 - 它具有非常严格的访问权限。

在您的特定情况下,此重新分析点针对可自由访问的 "C:\Users\{username}\AppData\Local\Microsoft\Windows\INetCache\IE" 文件夹。

为了与旧版软件兼容,用户文件夹中存在多个此类快捷方式。虽然您无法列出这些重解析点的内容,但您可以在知道名称后访问其中的文件和文件夹。

最后请注意,您无需在访问前检查特定的文件夹权限,相反您应该抓住 UnauthorizedAccessExpception 并采取相应的行动。你甚至不需要在访问之前检查文件夹是否存在,因为它可以在检查后删除(不是这个特定的文件夹,但通常),你只需要抓住 DirectoryNotFoundException.