company location

Setting File and Folder Permissions in VB.Net

Home | About Us | Products | Support | Contact Us | Library

How to copy permissions from one folder to another

Since writing the original guide to the method below we have discovered a better way to copy the NTFS permissions of a folder to another folder, which can actually transfer the effective permissions instead of just the explicit ones. This is obviously very handy in a range of situations and is done like so..

Private Sub match_folder_security(ByVal from_folder As String, ByVal to_folder As String, ByVal inherit As Boolean)

Dim from_folder_info As New DirectoryInfo(from_folder)

Dim to_folder_info As New DirectoryInfo(to_folder)

Dim fromfoldersecurity As DirectorySecurity = from_folder_info.GetAccessControl(AccessControlSections.Access)

Dim from_sddlform As String = fromfoldersecurity.GetSecurityDescriptorSddlForm(AccessControlSections.Access)

Dim tofoldersecurity As New DirectorySecurity

tofoldersecurity.SetSecurityDescriptorSddlForm(from_sddlform, AccessControlSections.Access)

If inherit.Equals(True) Then
tofoldersecurity.SetAccessRuleProtection(False, False)
Else If inherit.Equals(False) Then
tofoldersecurity.SetAccessRuleProtection(True, True)
End If

to_folder_info.SetAccessControl(tofoldersecurity)

End Sub