![](/img/trans.png)
[英]How we can use DirectorySecurity class for getting write access to a directory?
[英]Directory Not Found When trying get access rules using DirectorySecurity library
我正在嘗試使用一種獲取訪問規則並檢查已登錄用戶對該文件夾具有寫訪問權限的方法來為用戶提供特定的文件夾寫訪問權限。 但是,當嘗試執行此操作時,出現錯誤,指出directorynotfound。 以下是錯誤的屏幕截圖:
她是我正在使用的方法:
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;
}
有人對我要去哪里有什么建議嗎? 有一個更好的方法嗎?
您正在將path設置為字符串path = @“ PlanTemplate”的字符串值; 當我認為您想將plantemplate的值分配給您的路徑時。 按照此代碼的說法,當我認為您想要類似@“ c:\\ mydirectory”之類的內容時,它將始終嘗試評估名為“ PlanTemplate”的目錄。
更改:
string path = @"PlanTemplate";
至:
string path = PlanTemplate;
然后再試一次
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.