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
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
Comments
I shouldn't have to tell someone with over 100 posts how to use code tags.
This article has been dead for over six months. Start a new discussion instead.