繁体   English   中英

找不到目录尝试使用DirectorySecurity库获取访问规则时

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM