Mega Code Archive

 
Categories / VB.Net / File Directory
 

FileInfo GetAccessControl gets a FileSecurity object that encapsulates the access control list (ACL) entries

Imports System Imports System.IO Imports System.Security.AccessControl Module FileExample     Sub Main()         Try             Dim FileName As String = "c:\test.xml"             AddFileSecurity(FileName, "MyDomain\\MyAccessAccount", FileSystemRights.ReadData, AccessControlType.Allow)             RemoveFileSecurity(FileName, "MyDomain\\MyAccessAccount", FileSystemRights.ReadData, AccessControlType.Allow)         Catch e As Exception             Console.WriteLine(e)         End Try     End Sub     Sub AddFileSecurity(ByVal FileName As String, ByVal Account As String, ByVal Rights As FileSystemRights, ByVal ControlType As AccessControlType)         Dim fInfo As New FileInfo(FileName)         Dim fSecurity As FileSecurity = fInfo.GetAccessControl()         fSecurity.AddAccessRule(New FileSystemAccessRule(Account, Rights, ControlType))         fInfo.SetAccessControl(fSecurity)     End Sub     Sub RemoveFileSecurity(ByVal FileName As String, ByVal Account As String, ByVal Rights As FileSystemRights, ByVal ControlType As AccessControlType)         Dim fInfo As New FileInfo(FileName)         Dim fSecurity As FileSecurity = fInfo.GetAccessControl()         fSecurity.RemoveAccessRule(New FileSystemAccessRule(Account, Rights, ControlType))         fInfo.SetAccessControl(fSecurity)     End Sub End Module