Rauf
Rauf

Reputation: 12852

Registry management in C#

I would like to delete the following node from the registry. How can I do it?

HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{D9AC2148-5E15-48AD-A693-E48714592381}

I have this much:

string key = "D9AC2148-5E15-48AD-A693-E48714592381";
StringBuilder sb = new StringBuilder(key);
RegistryKey k = Registry.ClassesRoot.OpenSubKey("Wow6432Node\\CLSID", true);

How do I proceed?

Upvotes: 1

Views: 1603

Answers (2)

David Heffernan
David Heffernan

Reputation: 613441

You need to take into account registry redirection; that's the real issue here. On a 32-bit machine, the key you need doesn't have WoW6432Node in the path.

You are trying to delete

HKEY_CLASSES_ROOT\CLSID\{D9AC2148-5E15-48AD-A693-E48714592381}

from the 32-bit view of the registry. Microsoft is very clear that you should not hard code WoW6432Node in your applications:

Redirected keys are mapped to physical locations under Wow6432Node. For example, HKEY_LOCAL_MACHINE\Software is redirected to HKEY_LOCAL_MACHINE\Software\Wow6432Node. However, the physical location of redirected keys should be considered reserved by the system. Applications should not access a key's physical location directly, because this location may change.

So delete that key by calling

DeleteSubKey(@"HKEY_CLASSES_ROOT\CLSID\{D9AC2148-5E15-48AD-A693-E48714592381}")

But use the redirector to ensure that you operate on the 32-bit view of the registry.

In .NET you can achieve what you need in two ways.

  1. Target x86 and let redirection do the work for you.
  2. If you target x64 or AnyCPU, you need to use RegistryView.Registry32 (new in .NET 4) to open a 32-bit view of the registry. If you don't have .NET 4 then you have to P/Invoke.

Upvotes: 2

Darin Dimitrov
Darin Dimitrov

Reputation: 1039368

You could use the DeleteSubKey method:

string key = "{D9AC2148-5E15-48AD-A693-E48714592381}";
Registry.ClassesRoot.DeleteSubKey(@"Wow6432Node\CLSID\" + key);

Upvotes: 1

Related Questions