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. 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"; when I think you want to assign the value of plantemplate to your path. As this code stands, it will always try to evaluate a directory called "PlanTemplate" when I think you want something like @"c:\\mydirectory"
Change:
string path = @"PlanTemplate";
to:
string path = PlanTemplate;
and try again
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.