How to set folder write-access permissions

Heres my code

Dim FolderPath As String = "C:/folder"
Dim UserAccount As String = "(DESKTOP-T34R8TH\Users)"
Dim FolderInfo As IO.DirectoryInfo = New IO.DirectoryInfo(FolderPath)
Dim FolderAcl As New DirectorySecurity
FolderAcl.AddAccessRule(New FileSystemAccessRule(UserAccount, FileSystemRights.Modify, InheritanceFlags.ContainerInherit Or InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow))

nothing happens. i want the folder to have a write permission.

1 answer

  • answered 2018-03-13 20:52 DeanOC

    You can't add a new DirectorySecurity object to the folder. You have to retrieve the existing one and modify it.

    So instead of using

    Dim FolderAcl As New DirectorySecurity

    You should use

    Dim FolderAcl As DirectorySecurity = FolderInfo.GetAccessControl()

    See the MS documentation here for a comprehensive example.