none
Directory.GetAccessControl is incorrect, does n't give exact access rights of a user for a file RRS feed

  • Question

  • Hi

    In our windows application, we are checking for whether current user has write access to a folder or not.

    And the user have admin rights (write access) to that folder too.

    But using below code it shows that the user doesn't have write access.

    string destDir = "C:\Program Files (x86)\ABC";

    DirectorySecurity security = Directory.GetAccessControl(destDir);

    ty != null)
    {
    AuthorizationRuleCollection authRules = security.GetAccessRules(true, true, typeof(SecurityIdentifier));
    // Getting the identity of the current user principal <Checking the Windows group membership of the user>
    WindowsPrincipal principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
    if (authRules != null && authRules.Count > 0 && principal != null)
    {
     foreach (FileSystemAccessRule accessRule in authRules)
    {
    if (accessRule != null && accessRule.IdentityReference != null &&
     principal.IsInRole(accessRule.IdentityReference as SecurityIdentifier))
    {
    if ((FileSystemRights.WriteData & accessRule.FileSystemRights) == FileSystemRights.WriteData)
    {
    if (accessRule.AccessControlType == AccessControlType.Allow)
    return true;
    else if (accessRule.AccessControlType == AccessControlType.Deny)
    return false;
    }
    }
    }
    }
    }

    Can anyone suggest on this. Why it happens for few user but not for others.

    DirectorySecurity security = Directory.GetAccessControl(destDir);if (securi

    Friday, September 12, 2014 11:29 AM

Answers

  • Hello Kabila Krishnan,

    With your provided code, I reproduce this issue that my current login account does not have the write access for the new create folder although it is marked as the administrator which has the full control permission. The reason I find is that I open my Visio Studio directly so that it runs with my current login account which does not have a write access. After running the Visio Studio as the administrator, the code shows the excepted result: the write access is allowed.

    If these few users do not run the Visio Studio as the administrator, please check if the current login account has the permission to write data.

    Or having a try to run the VS with administrator to check if it would work.

    Best Regards,

    Fred.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, September 15, 2014 6:36 AM
    Moderator