Reputation: 165
I'm struggling with something really simple. I want to delete an entire registry key through a little console application. Application
is the key I want to delete, regardless of the values it contains:
This is what I did, but the subKey
value always seems to be null:
static void Main(string[] args)
{
string keyPath = "SOFTWARE\\Apps";
var hklm = Registry.LocalMachine;
var subKey = hklm.OpenSubKey(keyPath, true);
if(subKey != null)
{
subKey.DeleteSubKey("Application");
Console.WriteLine("DELETED");
}
else
{
Console.WriteLine("NOT FOUND");
}
}
Is there something obvious I'm missing? I've already looked for other answers but no luck at all.
Upvotes: 1
Views: 273
Reputation: 165
Thanks to the suggestions in the comments, I managed to delete the key from the registry. What I was missing was to explicitly tell the system I wanted to use the 64-bit version registry view.
I had to change the code as the following:
static void Main(string[] args)
{
string keyPath = "SOFTWARE\\Apps";
var subKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).OpenSubKey(keyPath, true); // This method accepts the RegistryView parameter.
if (subKey != null)
{
subKey.DeleteSubKey("Application");
Console.WriteLine("DELETED");
}
else
{
Console.WriteLine("NOT FOUND");
}
}
Upvotes: 1