0

我正在尝试制作一个通用函数来设置注册表用户权限。

问题是,如果我使用代码拒绝对密钥的完全控制访问,那么如果我再次允许它,它会在尝试读取此行的密钥时引发异常:

    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
4

1 回答 1

0

前段时间我写了一个小程序来授予对给定Registry Key. 它总是工作得很好。它的代码与您的代码相同,除了:

RS.AddAccessRule(New RegistryAccessRule(UserName, RegistryRights.CreateSubKey Or RegistryRights.Delete Or RegistryRights.ExecuteKey Or RegistryRights.FullControl Or RegistryRights.ReadKey Or RegistryRights.SetValue Or RegistryRights.WriteKey, AccessControlType.Allow))

不确定您的代码是否有任何变化(您正在使用我不知道其内容的变量)。在任何情况下,请注意,此应用在更改安全级别后从未访问过给定的密钥。该过程是:首先执行设置对密钥的完全访问权限;访问修改后的密钥的不同执行。

于 2013-07-21T15:45:11.833 回答