我正在尝试制作一个通用函数来设置注册表用户权限。
问题是,如果我使用代码拒绝对密钥的完全控制访问,那么如果我再次允许它,它会在尝试读取此行的密钥时引发异常:
Dim RK As RegistryKey = RootKey.OpenSubKey(KeyPath, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.ChangePermissions Or RegistryRights.ReadKey)
我这样调用程序:
RegEdit.Set_UserAccess_Key("hkcu\appevents", RegistryRights.FullControl, AccessControlType.Deny)
这是有效的,之后我称之为:
RegEdit.Set_UserAccess_Key("hkcu\appevents", RegistryRights.FullControl, AccessControlType.Allow)
当我尝试允许以前被拒绝的权限时的异常:
Requested registry access denied
Acceso denegado al Registro solicitado.
这是代码:
''' <summary>
''' Modify the User permissions of a registry key.
''' </summary>
Public Shared Sub Set_UserAccess_Key(ByVal RegKey As String, _
ByVal RegUserAccess As RegistryRights, _
ByVal AccessType As AccessControlType, _
Optional ByVal UserName As String = Nothing)
If UserName = Nothing Then UserName = Environment.UserDomainName & "\" & Environment.UserName
Dim RootKey As RegistryKey = Get_Root_Key(RegKey) ' Registrykey.Currentuser
Dim KeyPath As String = Get_Key_Path(RegKey) ' AppEvents
Dim RS As New RegistrySecurity()
Dim RK As RegistryKey = RootKey.OpenSubKey(KeyPath, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.ChangePermissions Or RegistryRights.ReadKey)
RS.AddAccessRule(New RegistryAccessRule(UserName, RegUserAccess, InheritanceFlags.None, PropagationFlags.None, AccessType))
RK.SetAccessControl(RS)
RK = RootKey.OpenSubKey(KeyPath, RegistryKeyPermissionCheck.ReadWriteSubTree, RegUserAccess)
RK.SetAccessControl(RS)
End Sub