Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a registry value in C#

Tags:

c#

registry

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?

like image 951
ebattulga Avatar asked Feb 10 '09 05:02

ebattulga


People also ask

How do I delete registry keys?

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.

How do I delete a reg key from a .reg file?

To delete a registry key with a . reg file, put a hyphen (-) in front of the RegistryPath in the . reg file.

How do I delete a subkey?

To delete a single subkey put a hyphen (-) after the equals sign following the DataItemName in the .

How do I change the value of data in the registry?

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.


1 Answers

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.

like image 158
Jon Skeet Avatar answered Sep 24 '22 21:09

Jon Skeet