Playing with Registry Keys might be dangerous for your Windows but if you are sure you want to delete a Registry key or one of its values, then here is a simple guide for you to achieve this using C# code.
Instructions:
- Start by adding reference to Microsoft.Win32 namespace.
using Microsoft.Win32; - Now its important, if you want to delete a Registry Key, you have to reference its Parent Registry and then delete a Sub Key of that Parent Registry Key which will be the Registry Key you want to delete. Say I have a Registry Key named HowToIdeas under CurrentUser (See this post where I have posted about the Top level Registry Keys http://howtoideas.net/how-to-read-value-from-registry-key-using-c), then I have to reference this CurrentUser (Top Level Registry Entry) and then delete its subkey which will be HowToIdeas as follows
RegistryKey rk = Registry.CurrentUser;
rk.DeleteSubKey("HowToIdeas"); - Now what about if we want to delte just a single value from a Registry Key not that whole Registry Key. In that case, we have to reference to our Registry Key then use a method DeleteValue() of RegistryKey class which will delete only one value from that Registry.
RegistryKey rk = Registry.CurrentUser;
RegistryKey rk1 = rk.OpenSubKey("HowToIdeas”);
rk1.DeleteValue("newField"); - If you are getting some error regarding Registry Access, then you have to give access to your Program to change Registry entries. You can take a look at my other article for this. How To Take Registry Access In C#
You can download my sample project in which I have completed 12 task on reading, writing, searching, deleting and creating Registry Entries. Click Here To Download Sample Project.
Incoming search terms:
- c# deletesubkey (1)
- register key in c net (1)