jondinham
jondinham

Reputation: 8511

How to create a registry key which can be accessed by any user, any application in C#?

I'm creating a registry key like this:

Key = Registry.CurrentUser.CreateSubKey("Mykey");

Then open it when my application runs the second time:

Key = Registry.CurrentUser.OpenSubKey("Mykey");

But I got an access denied when trying to create its subkey:

Subkey = Key.CreateSubKey("Mysubkey"); <-- Runtime error here

Any suggestion why? I guess it's because I didn't set the permissions on 'Mykey' at the time it is created. But I just don't know how to set these permissions.

Upvotes: 0

Views: 509

Answers (2)

jondinham
jondinham

Reputation: 8511

The answer is that the 'write' permission must be added to 'OpenSubKey' as a boolean value 'true', like this:

Key = Registry.CurrentUser.OpenSubKey("Mykey",true);

And it gives more permissions when doing a CreateSubKey by adding a parameter:

Key = Registry.CurrentUser.CreateSubKey("Mykey",
RegistryKeyPermissionCheck.ReadWriteSubTree);

However, this doesn't guarantee the key is accessible by any user, any application. And more, CreateSubKey also does the job for OpenSubKey when the key is already existing.

Upvotes: 0

Aman
Aman

Reputation: 548

You can set your app to run on full trust in your project properties, also, you can have your assemblies signed with a key from the "Signing" tab on the project properties page. Finally, run your app as an administrator and set it to run on admin mode by right clicking on the app launch icon and going into Compatibility tab and checking the "Run this program as an Administrator" checkbox. Registry handling requires full trust on the app to run properly. Hope this helps.

EDIT: Or alternatively, as people have mentioned, use local settings.

Upvotes: 1

Related Questions