我希望能够让我的用户获得他们无法访问的文件夹的读/写权限,而无需他们做额外的工作。我希望盒子出现在下面,然后他们可以简单地点击"继续"然后程序就会进展。
我可以使用
显示该框Process.Start(filePath);
然后那也会打开文件夹,这有点笨重。有没有办法只显示这个对话框,等待用户进行交互,然后继续执行?
答案 0 :(得分:0)
在这种情况下我会做什么:
文件的测试权限,您可以添加所需的内容(读取,执行,遍历)
public static bool HasWritePermissionOnDir(string path)
{
var writeAllow = false;
var writeDeny = false;
var accessControlList = Directory.GetAccessControl(path);
if (accessControlList == null)
return false;
var accessRules = accessControlList.GetAccessRules(true, true,
typeof(System.Security.Principal.SecurityIdentifier));
if (accessRules ==null)
return false;
foreach (FileSystemAccessRule rule in accessRules)
{
if ((FileSystemRights.Write & rule.FileSystemRights) != FileSystemRights.Write)
continue;
if (rule.AccessControlType == AccessControlType.Allow)
writeAllow = true;
else if (rule.AccessControlType == AccessControlType.Deny)
writeDeny = true;
}
return writeAllow && !writeDeny;
}
然后如果没有访问
// Add the access control entry to the file.
AddFileSecurity(fileName, @"DomainName\AccountName",
FileSystemRights.ReadData, AccessControlType.Allow);
FileSystemRights - 根据需要设置source
将所有操作放在try-catch块中,因为处理文件总是有问题: - )
确保您的用户有权更改权限(以管理员身份运行)