FileSecurity Class represents the access control and audit security for a file : FileSecurity « File Directory « VB.Net






FileSecurity Class represents the access control and audit security for a file

 

Imports System
Imports System.IO
Imports System.Security.AccessControl
Module FileExample
    Sub Main()
        Try
            Dim fileName As String = "test.xml"
            Console.WriteLine("Adding access control entry for " & fileName)
            AddFileSecurity(fileName, "DomainName\AccountName",FileSystemRights.ReadData, AccessControlType.Allow)

            Console.WriteLine("Removing access control entry from " & fileName)
            RemoveFileSecurity(fileName, "DomainName\AccountName",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 fSecurity As FileSecurity = File.GetAccessControl(fileName)
        Dim accessRule As FileSystemAccessRule = New FileSystemAccessRule(account, rights, controlType)
        fSecurity.AddAccessRule(accessRule)
        File.SetAccessControl(fileName, fSecurity)
    End Sub
    Sub RemoveFileSecurity(ByVal fileName As String, ByVal account As String,ByVal rights As FileSystemRights, ByVal controlType As AccessControlType)
        Dim fSecurity As FileSecurity = File.GetAccessControl(fileName)
        fSecurity.RemoveAccessRule(New FileSystemAccessRule(account,rights, controlType))
        File.SetAccessControl(fileName, fSecurity)

    End Sub
End Module

   
  








Related examples in the same category

1.FileSecurity Class Represents security for a file