Question

I have a annoying problem. I am trying to delete a registry value (no My Namespace) using the RegDeleteKey Value API, but with one modification. I want the function have an absolute path as parameter. So something like this:

RegDeleteKeyValue("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run","myValue")

Atm it looks like this (This one works, but has no absolute path as parameter, like it should be):

Private Enum RegHive
        HKEY_CLASSES_ROOT = &H80000000
        HKEY_CURRENT_USER = &H80000001
        HKEY_LOCAL_MACHINE = &H80000002
        HKEY_USERS = &H80000003
        HKEY_CURRENT_CONFIG = &H80000005
    End Enum

RegDeleteKeyValue(Reghive.HKEY_CURRENT_USER,"Software\Microsoft\Windows\CurrentVersion\Run","Myvalue")

I tried it to modify the function like this, so the parameters will only be the absolute path and the registryvalue.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    DeleteReyKeyValue("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run", "123")
End Sub

Public Shared Function DeleteReyKeyValue(ByVal Key As String, ByVal valueName As String) As Long
    Dim Handle As IntPtr
    Dim Hive As String = Split(Key, "\")(0)
    Select Case Hive
        Case "HKEY_CLASSES_ROOT"
            Handle = CType(&H80000000, IntPtr)
        Case "HKEY_CURRENT_USER"
            Handle = CType(&H80000001, IntPtr)
        Case "HKEY_LOCAL_MACHINE"
            Handle = CType(&H80000002, IntPtr)
        Case "HKEY_USERS"
            Handle = CType(&H80000003, IntPtr)
        Case "HKEY_CURRENT_CONFIG"
            Handle = CType(&H80000005, IntPtr)
    End Select
    Key = Key.Replace(Hive, String.Empty)
    Return RegDeleteKeyValue(Handle, Key, valueName)
End Function

<DllImport("advapi32.dll")> _
Private Shared Function RegDeleteKeyValue(ByVal handle As IntPtr, ByVal keyName As String, ByVal valueName As String) As Long
End Function

When using this, nothing happens, no error, but the Registryvalue gets not deleted? ;O Why is that?

Was it helpful?

Solution

The question is already answered in stackoverflow

So to delete your value, you should use this code,

string keyName = @"Software\Microsoft\Windows\CurrentVersion\Run";
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(keyName, true))
{
    if (key == null)
    {
        // Key doesn't exist. Do whatever you want to handle
        // this case
    }
    else
    {
        key.DeleteValue("123");
    }
}

This would delete the value from registry. I am not sure why you are adding such complex codes. Here is a link to msdn : http://social.msdn.microsoft.com/Forums/vstudio/en-US/5b22e94c-37a9-4be5-ad55-3d9229220194/how-to-use-add-read-change-delete-registry-keys-with-vbnet?forum=vbgeneral

It has the codes for VB but still it is nearly the same for c#, you can use a online converter to convert the codes, http://converter.telerik.com/

OTHER TIPS

Thanks for your quick reply. In that case a friend helped me out, code is now working. I was only missing a + "\" This works now:

 Public Shared Function DeleteReyKeyValue(ByVal Key As String, ByVal valueName As String) As Long
    Dim Handle As IntPtr
    Dim Hive As String = Split(Key, "\")(0)
    MsgBox("Hive:" & Hive)
    Select Case Hive
        Case "HKEY_CLASSES_ROOT"
            Handle = CType(&H80000000, IntPtr)
        Case "HKEY_CURRENT_USER"
            Handle = CType(&H80000001, IntPtr)
        Case "HKEY_LOCAL_MACHINE"
            Handle = CType(&H80000002, IntPtr)
        Case "HKEY_USERS"
            Handle = CType(&H80000003, IntPtr)
        Case "HKEY_CURRENT_CONFIG"
            Handle = CType(&H80000005, IntPtr)
    End Select
    Key = Key.Replace(Hive + "\", String.Empty)
    Return RegDeleteKeyValue(Handle, Key, valueName)
End Function
<DllImport("advapi32.dll")> _
Private Shared Function RegDeleteKeyValue(ByVal handle As IntPtr, ByVal keyName As String, ByVal valueName As String) As Long
End Function

Credits: Rajin

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top