Testing a UNC Path's “Accessability”

后端 未结 2 2010
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-18 19:27

My program requires access to a certain UNC path, but the path is cross-domain, so depending on the machine the program is being run on, it may or may not have default crede

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-18 20:23

    Check these articles:

    • Testing File Access Rights in .NET 2.0. A handy class for testing file system permissions.
    • Vexing exceptions. Eric Lippert on why pre-emptive testing is pointless (in a nutshell: there's an implicit race condition. Between the test and the actual access, some other process may change permissions, move or delete the file, or the network could drop...etc. This means you've got to deal with exceptions thrown when you try to access the resource, so you might well structure your code to deal with the problem when it actually occurs.

    I've used code like this:

    new FileIOPermission(FileIOPermissionAccess.Read, path).Demand();
    

    which is supposed to throw a SecurityException if you don't have the desired access, but for paths on network drives or UNC paths, the FileIOPermission.Demand() method appears to be a no-op.

提交回复
热议问题