Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding registry key in C# shows when I read it back, but not in regedit

I am adding a registry key using the following code:

var key = Microsoft.Win32.Registry.LocalMachine.CreateSubKey(key);

Within my code I can read back the value find, even in between runs. However, the key never shows in regedit and the other program that should be reading the key can't see it.

The program is running on Vista with elevated priviledges.

like image 519
ICR Avatar asked Oct 08 '09 23:10

ICR


People also ask

How do I add a registry key?

Once you've located the registry key you want to add to, you can add the key or value you want to add: If you're creating a new registry key, right-click or tap-and-hold on the key it should exist under and choose New > Key. Name the new registry key and then press Enter.

What is registry key in C#?

You can consider registry keys are folders in your windows system. Note that a key can have sub keys - much the same way a folder can contain sub folders inside it. To work with the Windows Registry using C#, you can take advantage of the Registry class in the Microsoft. Win32 namespace.

What is RegCreateKeyEx?

The RegCreateKeyEx function creates all missing keys in the specified path. An application can take advantage of this behavior to create several keys at once.


2 Answers

Vista introduced registry virtualization; "global" registry changes are, in some cases, redirected to user-specific locations:

Registry virtualization is an application compatibility technology that enables registry write operations that have global impact to be redirected to per-user locations. This redirection is transparent to applications reading from or writing to the registry. It is supported starting with Windows Vista.

This form of virtualization is an interim application compatibility technology; Microsoft intends to remove it from future versions of the Windows operating system as more applications are made compatible with Windows Vista. Therefore, it is important that your application does not become dependent on the behavior of registry virtualization in the system.

You can disable this by customizing your application manifest, or you can modify policy for just the key you're touching.

WOW64 (are you running a 64-bit edition of Vista?) also does registry redirection. This has bitten me in annoying ways.

See the MSDN article on registry virtualization.

like image 150
Michael Petrotta Avatar answered Nov 14 '22 21:11

Michael Petrotta


This sounds like a registry virtualisation issue. If the user of your app is not an administrator, then the registry write will be virtualised into a per-user virtual store -- though if your other program is running under the same account then it should still be able to see that per-user setting...

The linked page tells you where to find the virtual store to check this theory.

like image 39
itowlson Avatar answered Nov 14 '22 22:11

itowlson