none
Access denied exception occurred, When I am trying setting the hidden attributes to some folder. RRS feed

  • Question

  • hi, 

    I am facing issue while setting the hidden attribute for the particular folder by non admin users. But the same code is working in some machine with non admin privilege. Actually I am not trying to set the hidden attributes for system folder. Is there any other settings available to perform before set file attributes?


               

    string basePath="C:\Folder1\Folder2";

    if (Directory.Exists(basePath))
                {
                    DirectoryInfo di = new DirectoryInfo(basePath);
                    di.Attributes = FileAttributes.Directory | FileAttributes.Hidden | FileAttributes.System | FileAttributes.Normal;
                    //di.Attributes &= ~FileAttributes.ReadOnly;
                }

    Thanks

    Arivazhagan K

    Tuesday, November 14, 2017 12:28 PM

All replies

  • This error indicates you don't have permissions to change the security on that item. There is nothing to do but ensure the user has permissions to change the settings first by looking at the NTFS permissions. 

    Note that your basePath cannot possibly be right so I assume this is just an example. But nevertheless check the NTFS permissions on the folder.

    Note also that you shouldn't be setting the attributes in the matter you specified. You shouldn't explicitly set the attribute values as that would overwrite what is already there. To set a directory as hidden just OR the hidden attribute in.

    di.Attributes |= FileAttributes.Hidden;
    You also shouldn't be setting the System (and Normal would collide with all that). You also cannot set Directory as it is already a directory.


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, November 14, 2017 3:33 PM
    Moderator
  • Hello Arivazhagan,

    First you should check if you have enough permissions to modify the folder attribute. you could see it by below steps.

    right click folder >> select properties options >> choose security tab you will see the permission for users

    And even if you are an administrator but you do not have permission set, you still And even if you are an administrator but you don't have permission set, you still can't access . I suggest you try to change folder attribute manually . Once it succeed , the code also could make it.

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 15, 2017 8:09 AM
    Moderator
  • Hello Arivazhagan,

    Did you have anything to update?  If you already have got the useful solution from there, you could close the thread by marking the useful reply  as answer, This can be beneficial to other community members reading this thread. Thanks a lot.

    Sincerely,
    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, November 26, 2017 11:54 AM
    Moderator
  • And even if you are an administrator but you do not have permission set, you still And even if you are an administrator but you don't have permission set, you still can't access . I suggest you try to change folder attribute manually . Once it succeed , the code also could make it.

    In regards to the above, another user group's permissions on a file or folder can supersede admin rights on the folder, like Users can supersede admin rights.

    Sunday, November 26, 2017 12:58 PM