Search code examples
vb.netregistryedit

VB.NET - Edit Windows Registry Value


I'm trying to set my program as the "Default Program" for all Text Files so in order to do this I need to change the (Default) value which is: %SystemRoot%\system32\NOTEPAD.EXE %1 to C:\Program.exe %1 in HKEY_CLASSES_ROOT\txtfile\shell\open\command\

I do this by using this code:

        Dim regKey As RegistryKey
        regKey = Registry.ClassesRoot.OpenSubKey("txtfile\shell\open\command", True)
        regKey.SetValue("(Default)", "C:\Program.exe %1", RegistryValueKind.ExpandString)
        regKey.Close()

The problem is that when I do it, it creates another key with the Expand String called "(Default)" rather than editing the "(Default)" key which is already there. How can I EDIT rather than just create?


Solution

  • This is explicitly mentioned in a NOTE in the MSDN article for RegistryKey.SetValue():

    A registry key can have one value that is not associated with any name. When this unnamed value is displayed in the registry editor, the string "(Default)" appears instead of a name. To set this unnamed value, specify either null or the empty string ("") for the name.

    Fix:

        regKey.SetValue("", "C:\Program.exe %1", RegistryValueKind.ExpandString)