下面的代码示例使用 GetAccessControl 方法和 SetAccessControl 方法向一个文件添加访问控制列表 (ACL) 项,然后将其从该文件中移除。您必须提供有效的用户或组帐户才能运行此示例。
- 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, "MYDOMAIN\MyAccount", FileSystemRights.ReadData, AccessControlType.Allow)
-
- Console.WriteLine("Removing access control entry from " & FileName)
-
-
- RemoveFileSecurity(FileName, "MYDOMAIN\MyAccount", FileSystemRights.ReadData, AccessControlType.Allow)
-
- Console.WriteLine("Done.")
- Catch e As Exception
- Console.WriteLine(e)
- End Try
-
- Console.ReadLine()
-
- 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
|