I am trying to delete a registry key, so far I tried that code which seems to be normal :
RegistryKey delete = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Image File Execution Options");
delete.DeleteSubKeyTree("MyPaintApp");
delete.Close();
But I get the ERROR :
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
I googled my problem and searched stackoverflow and applied some solutions , but all seem to be using the same method I am using, so I’m fed up and I hope you can help me with that.
EDIT : SORRY I used CurrentUser instead of LocalMachine in my registryKey path , that was the problem.
The documentation for
OpenSubKeystates:Thus opening the key seems to have failed. most likely because it doesn’t exist:
On my Win7 comp, the
Image File Execution Optionssubkey doesn’t exist.But even when you fix that part, it’ll still fail. The overload of
OpenSubKeyyou are using is documented as:So you should use
OpenSubKey(path,true), like @lasseespeholt recommends.http://msdn.microsoft.com/en-us/library/z9f66s0a.aspx