简体   繁体   English

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

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

相关问题 我们如何使用DirectorySecurity类获得对目录的写访问权限? - How we can use DirectorySecurity class for getting write access to a directory? 尝试在IIS 8.5上使用HTTPS访问目录但无法使用HTTP时,找不到404服务器错误资源 - Server Error Resource cannot be found 404 when trying to access directory using HTTPS on IIS 8.5 but works using HTTP 为什么我在尝试清除目录的内容时收到消息“拒绝访问路径 'bootmgr'” - Why do I get message “Access to the path 'bootmgr' is denied ” when trying to clear the contents of a directory 当对存在的目录使用 ListDirectoryDetails 时,FtpWebRequest 返回“550 文件不可用(例如文件未找到,无法访问)” - FtpWebRequest returning "550 File unavailable (e.g. file not found, no access)" when using ListDirectoryDetails for a directory that exists 尝试路由到特定URL时找不到目录错误 - Directory not found error when trying to route to a specific URL CreateDirectory是否在现有目录上应用DirectorySecurity? - Does CreateDirectory apply DirectorySecurity on pre-existing directory? 尝试将文件保存到目录时访问被拒绝错误 - Access denied error when trying to save file to directory 尝试访问活动目录时出现DirectoryServicesCOMException(0x80072030) - DirectoryServicesCOMException (0x80072030) when trying to access active directory 尝试使用IIS 5.1访问HttpHandler时找不到页面 - Page not found when trying to access HttpHandler with IIS 5.1 用户具有完全控制权时,DirectorySecurity.SetAccesesControl上的UnauthorizedAcccesException - UnauthorizedAcccesException on DirectorySecurity.SetAccesesControl when user has full control
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM