【发布时间】:2010-11-27 11:07:36
【问题描述】:
在实际尝试之前,我需要测试用户是否可以写入文件夹。
我已经实现了以下方法(在 C# 2.0 中),它尝试使用 Directory.GetAccessControl() 方法检索文件夹的安全权限。
private bool hasWriteAccessToFolder(string folderPath)
{
try
{
// Attempt to get a list of security permissions from the folder.
// This will raise an exception if the path is read only or do not have access to view the permissions.
System.Security.AccessControl.DirectorySecurity ds = Directory.GetAccessControl(folderPath);
return true;
}
catch (UnauthorizedAccessException)
{
return false;
}
}
当我在谷歌上搜索如何测试写访问权限时,没有出现类似的情况,在 Windows 中实际测试权限似乎非常复杂。我担心我过度简化了事情,而且这种方法并不可靠,尽管它似乎确实有效。
我测试当前用户是否有写权限的方法能正常工作吗?
【问题讨论】:
-
没有访问权限view真的和不允许写一样吗?
标签: c# permissions directory