52

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ebattulga
  • 10,774
  • 20
  • 78
  • 116

6 Answers6

109

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
18

To delete all subkeys/values in the tree (~recursively), here's an extension method that I use:

public static void DeleteSubKeyTree(this RegistryKey key, string subkey, 
    bool throwOnMissingSubKey)
{
    if (!throwOnMissingSubKey && key.OpenSubKey(subkey) == null) { return; }
    key.DeleteSubKeyTree(subkey);
}

Usage:

string keyName = @"Software\Microsoft\Windows\CurrentVersion\Run";
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(keyName, true))
{
   key.DeleteSubKeyTree("MyApp",false);   
}
Even Mien
  • 44,393
  • 43
  • 115
  • 119
  • 7
    Looks a like some one working on .NET thought this was a good idea too :) Was added for .NET 4.0 http://msdn.microsoft.com/en-us/library/dd411622.aspx – Darren Reid Nov 11 '12 at 02:20
  • 1
    Just a point to note, if the second argument in `DeleteSubKeyTree()` is not specified it will assume it true and will throw exception unless the key is closed after `OpenSubKey()` is called. – Sisir Jul 30 '18 at 13:22
15
RegistryKey registrykeyHKLM = Registry.LocalMachine;
string keyPath = @"Software\Microsoft\Windows\CurrentVersion\Run\MyApp";

registrykeyHKLM.DeleteValue(keyPath);
registrykeyHKLM.Close();
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Binoj Antony
  • 15,886
  • 25
  • 88
  • 96
1

RegistryKey.DeleteValue

Sören Kuklau
  • 19,454
  • 7
  • 52
  • 86
0
 string explorerKeyPath = @"Software\TestKey";
 using (RegistryKey explorerKey = Registry.CurrentUser.OpenSubKey(explorerKeyPath, writable: true))
 {
     if (explorerKey != null)
     {
         explorerKey.DeleteSubKeyTree("TestSubKey");
     }
 }
Shivaraj R
  • 39
  • 2
  • 2
    Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel Jul 29 '20 at 13:17
0

I Needed something a little different, I just needed to delete everything a key contained. Therefore the below

Registry.LocalMachine.DeleteSubKeyTree(@"SOFTWARE\YourNeededKeyThatHasMany\");

Note that here its using LocalMachine so it's looking in "HKEY_LOCAL_MACHINE" for the Key to delete its SubTreeKeys. Was simpler for me to do this and would've liked to see this simple answer here.

e1337
  • 1