Saturday, June 2, 2007

Setting NTFS Directory/Drive permissions in VB.NET 2005

It seems setting File Permissions in 2005 is easy, but there didn't seem to be much regarding directory or drive ACL's. So a little digging and adapting goes a long way .....

Dim folder_info As New DirectoryInfo("C:\")
' Read the current ACL
Dim foldersecurity As DirectorySecurity = folder_info.GetAccessControl _
(AccessControlSections.Access)

' Make up my new ACE
Dim MyRule As New System.Security.AccessControl.FileSystemAccessRule _
("DOMAIN\USER", FileSystemRights.FullControl, AccessControlType.Allow)

' Add it to the old ACL
foldersecurity.AddAccessRule(MyRule)
' Apply it
folder_info.SetAccessControl(foldersecurity)

No comments: