0

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:

enter image description here

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?

Programmermid
  • 588
  • 3
  • 9
  • 30

1 Answers1

2

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

Shannon Holsinger
  • 2,293
  • 1
  • 15
  • 21
  • Thanks Shannon that worked. But now its giving an error stating Unauthorized Operation. I checked the file access & other things and everything looks fine. Is there any other thing that we need to check when working with DirectorySecurtiy Library? – Programmermid Sep 15 '16 at 14:42
  • Same line: DirectorySecurity acl = di.GetAccessControl(AccessControlSections.All); – Programmermid Sep 15 '16 at 15:15
  • Have a look here: http://stackoverflow.com/questions/1281620/checking-for-directory-and-file-write-permissions-in-net sometimes, you can't get AccessControl. There are some suggestions in this article about what to do in such an instance. – Shannon Holsinger Sep 15 '16 at 15:19