大家都知道权限的概念吧,在一个后台你可能有的权限仅仅就一个,在这里我们来讲讲关于VB.NET注册表权限的例子。
本实例需要项目引用:
- Imports Microsoft.Win32 '用途 : 注册表操作
- Imports System.Security.AccessControl'用途 : 访问权限控制
首先,对VB.NET注册表权限增加,细分起来共有11种可选的权限类型,它们对应的参数如下:
- Select Case ComboBox1.Text
- Case "完全控制"
- ObjRegRight = RegistryRights.FullControl
- Case "查询数值"
- ObjRegRight = RegistryRights.QueryValues
- Case "设置数值"
- ObjRegRight = RegistryRights.SetValue
- Case "创建子项"
- ObjRegRight = RegistryRights.CreateSubKey
- Case "枚举子项"
- ObjRegRight = RegistryRights.EnumerateSubKeys
- Case "通知"
- ObjRegRight = RegistryRights.Notify
- Case "创建链接"
- ObjRegRight = RegistryRights.CreateLink
- Case "删除"
- ObjRegRight = RegistryRights.Delete
- Case "写入DAC"
- ObjRegRight = RegistryRights.WriteKey
- Case "写入所有者"
- ObjRegRight = RegistryRights.TakeOwnership
- Case "读取控制"
- ObjRegRight = RegistryRights.ReadPermissions
- End Select
而每个细分权限 又分"允许"和"拒绝"两种访问控制类型
- Select Case ComboBox2.Text
- Case "允许"
- ObjRegAccess = AccessControlType.Allow
- Case "拒绝"
- ObjRegAccess = AccessControlType.Deny
- End Select
以下为增加VB.NET注册表权限的函数
以下两函数中 Account代表系统nt帐户 Rights和ControlType分别为上文提及的权限类型和访问控制类型
- Private Sub AddRegistrySecurity(ByVal Str_FileName As String, ByVal Account As String, ByVal Rights As RegistryRights, ByVal ControlType As AccessControlType)
- Dim RegKey As RegistryRegistryKey = Registry.CurrentUser.CreateSubKey("此处填写具体键地址")
- Dim RegkeyAcl As RegistrySecurity = RegKey.GetAccessControl()
- Dim AccessRule As RegistryAccessRule = New RegistryAccessRule(Account, Rights, ControlType)
- RegkeyAcl.AddAccessRule(AccessRule)
- RegKey.SetAccessControl(RegkeyAcl)
- RegKey.Close()
- End Sub
以下为移除注册表键权限的函数
- Private Sub RemoveRegistrySecurity(ByVal Str_FileName As String, ByVal Account As String, ByVal Rights As RegistryRights, ByVal ControlType As AccessControlType)
- Dim RegKey As RegistryRegistryKey = Registry.CurrentUser.CreateSubKey("此处填写具体键地址")
- Dim RegkeyAcl As RegistrySecurity = RegKey.GetAccessControl()
- Dim AccessRule As RegistryAccessRule = New RegistryAccessRule(Account, Rights, ControlType)
- RegkeyAcl.RemoveAccessRule(AccessRule)
- RegKey.SetAccessControl(RegkeyAcl)
- RegKey.Close()
- End Sub
【编辑推荐】