[英]Directory Not Found When trying get access rules using DirectorySecurity library
I am trying to get a particular folder write access for users using a method that gets access rules and checks wheather the logged in user has write access for that folder. 我正在尝试使用一种获取访问规则并检查已登录用户对该文件夹具有写访问权限的方法来为用户提供特定的文件夹写访问权限。 But when trying to do this I am getting an error stating directorynotfound.
但是,当尝试执行此操作时,出现错误,指出directorynotfound。 Below is the screenshot of the error:
以下是错误的屏幕截图:
Her is the method I am using: 她是我正在使用的方法:
private bool AccessPlanTemplate()
{
bool result = false;
try
{
string PlanTemplate = System.Configuration.ConfigurationSettings.AppSettings["PlanTemplate"];
string path = @"PlanTemplate";
string NtAccountName = @"TestUser";
DirectoryInfo di = new DirectoryInfo(path);
DirectorySecurity acl = di.GetAccessControl(AccessControlSections.All); //I AM GETTING ERROR ON THIS LINE
System.Security.AccessControl.AuthorizationRuleCollection rules = acl.GetAccessRules(true, true, typeof(NTAccount));
//Go through the rules returned from the DirectorySecurity
foreach (System.Security.AccessControl.AuthorizationRule rule in rules)
{
//If we find one that matches the identity we are looking for
if (rule.IdentityReference.Value.Equals(NtAccountName, StringComparison.CurrentCultureIgnoreCase))
{
//Cast to a FileSystemAccessRule to check for access rights
if ((((FileSystemAccessRule)rule).FileSystemRights & FileSystemRights.WriteData) > 0)
{
result = true;
}
}
}
}
catch (UnauthorizedAccessException)
{
result = false;
}
return result;
}
Does somebody has any suggestion regarding Where am I going wrong? 有人对我要去哪里有什么建议吗? Is there a better way to do this?
有一个更好的方法吗?
You're setting path to a string value of string path = @"PlanTemplate"; 您正在将path设置为字符串path = @“ PlanTemplate”的字符串值; when I think you want to assign the value of plantemplate to your path.
当我认为您想将plantemplate的值分配给您的路径时。 As this code stands, it will always try to evaluate a directory called "PlanTemplate" when I think you want something like @"c:\\mydirectory"
按照此代码的说法,当我认为您想要类似@“ c:\\ mydirectory”之类的内容时,它将始终尝试评估名为“ PlanTemplate”的目录。
Change: 更改:
string path = @"PlanTemplate";
to: 至:
string path = PlanTemplate;
and try again 然后再试一次
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.