Directory.Exists()和访问权限 [英] Directory.Exists() and Access Permissions

查看:380
本文介绍了Directory.Exists()和访问权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到当我在另一个拒绝用户访问的文件夹下面的文件夹上调用'Directory.Exists()'时,即使该目录在技术上存在,它也会返回False。



例如,假设我有两个文件夹:



C:\ FirstFolder \

C: \ FirstFolder \SecondFolder \



如果我在C:\ FirstFolder \上放置一个安全条目,以便明确拒绝用户读取对该文件夹的权限,我无法在Windows资源管理器中的该文件夹下浏览(这是理解的行为)。



如果我写了一些调用'Directory.Exists(C:\ FirstFolder \SecondFolder \)'的代码,那么它返回False。此结果具有误导性,因为该目录确实存在。对于大多数其他IO操作(例如'Directory.Delete()'),我会得到'UnauthorizedAccessException'或'IOException'或类似的东西。为什么不从'Directory.Exists()'方法?



有没有人知道一个简单的方法呢?

I noticed that when I call 'Directory.Exists()' on a folder that is beneath another folder whose access is denied to the user, it returns False even though the directory technically exists.

For example, assume I have two folders:

C:\FirstFolder\
C:\FirstFolder\SecondFolder\

If I place a security entry on 'C:\FirstFolder\' so that the user is explicitly denied 'read' permissions on that folder, I cannot browse beneath that folder in Windows Explorer (which is the understood behavior).

If I write some code that calls 'Directory.Exists("C:\FirstFolder\SecondFolder\")', then it returns False. This result is misleading because the directory does actually exist. With most other IO operations (such as 'Directory.Delete()'), I would get an 'UnauthorizedAccessException' or an 'IOException' or something like that. Why not from the 'Directory.Exists()' method?

Does anyone know of an easy way around this?

推荐答案

这是我检查可列表但不可读的网络文件夹的简单解决方案:



Here is my simple solution for checking listable but unreadable network folders:

public static bool DirectoryVisible(string path)
{
    try
    {
        Directory.GetAccessControl(path);
        return true;
    }
    catch (UnauthorizedAccessException)
    {
        return true;
    }
    catch
    {
        return false;
    }
}


我认为没有一个简单的方法。从安全的角度来看,它是有道理的。如果不允许用户对目录进行读访问,则他们应该无法获得有关该目录的任何信息,包括子目录结构。另外它在哲学上是正确的,因为对于用户来说该目录不存在。
I don't think there is an easy way around this. From a security point of view it makes sense. If a user is not allowed read access to a directory, then they shouldn't be able to get any information about the directory at all including subdirectory structure. Also it's philosophically correct, because to the user the directory doesn't exist.


DirectoryInfo.Attributes属性被错误地记录,并且不会引发FileNotFound或DirectoryNotFound异常,而是返回错误值来自底层的win api函数,即0xFFFFFFFF或-1。



如果路径存在但不允许访问,则尝试检索属性将引发异常。



如果路径不存在则属性为-1。



示例

The DirectoryInfo.Attributes property is incorrectly documented and does not raise the FileNotFound or DirectoryNotFound exceptions but instead returns the error value from the underlying win api function, which is 0xFFFFFFFF or -1.

If a path exists but access is disallowed then an attempt to retrieve the attributes will throw an exception.

If the path does not exist then the attributes will be -1.

Examples
C:\Documents and Settings\Administrator\Desktop
  This is not accessible to a standard user account
  DirInfo.Exists = false
  DirInfo.Attributes throws Access Denied

C:\Documents and Settings\Administrator\Desktop\NoDir
  This does not exist and would not be accessible
  DirInfo.Exists = false
  DirInfo.Attributes  =  -1



测试代码(在XP专业版SP 3上运行)


Test Code (run on XP Pro SP 3)

    enum ExistState { exist, notExist, inaccessible };
    
    void Check(string name) {
      DirectoryInfo di = new DirectoryInfo(name);
      ExistState state = ExistState.exist;
      if (!di.Exists) {
        try {
          if ((int)di.Attributes == -1) {
            state = ExistState.notExist;
          }
        } catch (UnauthorizedAccessException) {
          state = ExistState.inaccessible;
        }
      }
      Console.WriteLine("{0} {1}", name, state);
    }

Output:
  C:\Documents and Settings\Administrator\Desktop inaccessible
  C:\Documents and Settings\Administrator\Desktop\NoDir notExist





我怀疑MS会在不久的将来修复属性属性失败的行为,但是说过我正在使用.NET Framework 2并假设在最近的版本中没有任何变化。



我在代码中可以看到的一个问题是它是如果状态值是不可访问,则无法知道名称是指文件还是目录。



Alan。



I doubt that MS will fix this failing of the Attributes property to behave as documented any time soon but having said that I'm using .NET Framework 2 and am assuming that nothing has changed in the more recent versions.

One problem that I can see in my code is that it is impossible to know whether name refers to a file or a directory if the state value is 'inaccessible'.

Alan.


这篇关于Directory.Exists()和访问权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆