Error is coming Registry Access is not allowed.While Opening a Key.
Can Somebody help me out.

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        NewDelete("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run", "Sonia")
    End Sub

    Private Sub NewDelete(ByVal KeyPath As String, ByVal KeyName As String)
        Dim sMainKey As String
        Dim sKeyPath As String
        Dim ObjReg As RegistryKey
        Dim sData As Object

       
        Try
           

            Dim mSec As New RegistrySecurity()
            Dim user As String = Environment.UserDomainName & "\" & Environment.UserName

            sMainKey = Mid(KeyPath, 1, InStr(KeyPath, "\", Microsoft.VisualBasic.CompareMethod.Text) - 1)
            ObjReg = GetMainKey(sMainKey)
            sKeyPath = Replace(KeyPath, sMainKey, "")
            Do While True
                If Mid(sKeyPath, 1, 1) = "\" Then
                    sKeyPath = Mid(sKeyPath, 2)
                Else
                    Exit Do
                End If
                Application.DoEvents()
            Loop

            Dim rule As New Security.AccessControl.RegistryAccessRule(user, _
            Security.AccessControl.RegistryRights.FullControl, _
            Security.AccessControl.AccessControlType.Allow)



            mSec.AddAccessRule(New RegistryAccessRule(user, RegistryRights.FullControl, _
           System.Security.AccessControl.InheritanceFlags.ContainerInherit, _
           PropagationFlags.None, AccessControlType.Allow))


            ObjReg = ObjReg.OpenSubKey(sKeyPath, True)
            ObjReg.SetAccessControl(mSec)
            If ObjReg IsNot Nothing Then ObjReg.Close()

            mSec.AddAccessRule(rule)
            'if Object .Equals (
            If Not ObjReg Is Nothing Then
                sData = Registry.GetValue(ObjReg.ToString, KeyName, "")
                If Not sData Is "" Then
                    ObjReg.DeleteValue(KeyName, False)
                End If
            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

Recommended Answers

All 6 Replies

Running on Vista?

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim a As RegistryPermission = New RegistryPermission( _
        RegistryPermissionAccess.AllAccess, "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\Sonia")
        a.Demand()
        NewDelete("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\Sonia", "ss")
    End Sub

    Private Sub NewDelete(ByVal KeyPath As String, ByVal KeyName As String)
        Dim sMainKey As String
        Dim sKeyPath As String
        Dim ObjReg As RegistryKey
        Dim sData As Object


        Try





            sMainKey = Mid(KeyPath, 1, InStr(KeyPath, "\", Microsoft.VisualBasic.CompareMethod.Text) - 1)
            ObjReg = Registry.LocalMachine
            sKeyPath = Replace(KeyPath, sMainKey, "")
            Do While True
                If Mid(sKeyPath, 1, 1) = "\" Then
                    sKeyPath = Mid(sKeyPath, 2)
                Else
                    Exit Do
                End If
                Application.DoEvents()
            Loop

            ObjReg = ObjReg.OpenSubKey(sKeyPath, True)
           
            If Not ObjReg Is Nothing Then
                sData = Registry.GetValue(ObjReg.ToString, KeyName, "")
                If Not sData Is "" Then
                    ObjReg.DeleteValue(KeyName, False)
                End If
            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
commented: I shouldn't have to tell someone with over 100 posts how to use code tags. -7

wtf is that? That doesn't answer his question.....

To run as administrator

No No, you asked if it was vista, and they posted code....what is that about?

LOL, I got it, I don't know, they may understand my question wrong :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.