I can get/set registry values using the Microsoft.Win32.Registry class. For example,
Microsoft.Win32.Registry.SetValue( @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run", "MyApp", Application.ExecutablePath);
But I can't delete any value. How do I delete a registry value?
Open the Registry Editor by selecting Start, Run, typing regedit and clicking OK. Navigate your way to HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall. In the left pane, with the Uninstall key expanded, right-click any item and select Delete.
To delete a registry key with a . reg file, put a hyphen (-) in front of the RegistryPath in the . reg file.
To delete a single subkey put a hyphen (-) after the equals sign following the DataItemName in the .
To change a value, double-click it in the value pane, or select it and then click Modify on the Edit menu. When you open a value, Regedit displays a different dialog box, depending on the value's type.
To delete the value set in your question:
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("MyApp"); } }
Look at the docs for Registry.CurrentUser
, RegistryKey.OpenSubKey
and RegistryKey.DeleteValue
for more info.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With