面的代码示例使用 FileSecurity 类向文件中添加访问控制列表 (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, "DomainName\AccountName", _
- FileSystemRights.ReadData, AccessControlType.Allow)
-
- Console.WriteLine("Removing access control entry from " & fileName)
-
-
- RemoveFileSecurity(fileName, "DomainName\AccountName", _
- FileSystemRights.ReadData, AccessControlType.Allow)
-
- Console.WriteLine("Done.")
- 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
|