Question

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.

Was it helpful?

Solution

The documentation for OpenSubKey states:

Return Value
Type: Microsoft.Win32.RegistryKey
The subkey requested, or null if the operation failed.

Thus opening the key seems to have failed. most likely because it doesn't exist:

If the specified subkey cannot be found, then null is returned.

On my Win7 comp, the Image File Execution Options subkey doesn't exist.


But even when you fix that part, it'll still fail. The overload of OpenSubKey you are using is documented as:

Retrieves a subkey as read-only.

So you should use OpenSubKey(path,true), like @lasseespeholt recommends.

http://msdn.microsoft.com/en-us/library/z9f66s0a.aspx

OTHER TIPS

Please try using Registry.CurrentUser.DeleteSubKeyTree(fullSubKeyPath);.

Details here: RegistryKey.DeleteSubKeyTree Method (String).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top