OpenBaseKey打开并更改只读注册表项

时间:2019-04-15 18:24:11

标签: vb.net permissions registry

我有一个注册表项,我的用户需要在我的VB.NET应用程序中访问它。我注意到某些用户系统仅对注册表项具有只读访问权限。我知道如何在regedit中更正此错误。但是,由于这是我程序不可或缺的一部分,所以我需要一种务实的方法来完成它。

在密钥上,管理员帐户设置为只读。

如果我可以打开密钥,则可以更改权限。

Private Sub Main()
    Dim ProductKey As String
    ProductKey = "Software\My Application"

    Dim ApplicationsKey As String
    ApplicationsKey = ProductKey & "\Applications"

    Dim AppKey As String
    AppKey = ApplicationsKey & "\MyApp"

    Dim LM As RegistryKey
    LM = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)

    Dim CRKey As RegistryKey
    CKey = LM.OpenSubKey(AppKey, True)
End Sub

如果opensubkey设置为可写true,并且权限已写入,则我可以打开并更改密钥权限。如果权限是只读的,则必须将opensubkey设置为false可打开密钥。如果可写设置为false,则无法更改权限。

Dim MyPath As String = "This is where registery key is"
Dim LM As RegistryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)
Dim MyKey As RegistryKey = LM.OpenSubKey(MyPath, False)
Dim Trip = MyKey.GetAccessControl.AreAccessRulesProtected
If MyKey IsNot Nothing Then
    If Trip = False Then
        MsgBox(MyKey.Name)
    Else
        MsgBox(MyKey.Name & " is read-only")

        'This is where the hicup is. You cannot OpenSubKey/Write=True a key you do not have rights to.
        'You cannot edit the rights if OpenSubKey/Write=False.
        'I have to manually change the users rights in regedit.

    End If
Else
    MsgBox(MyPath & " Not Found.")
End If

例程正在运行管理员。

0 个答案:

没有答案
相关问题