Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to create or read registry key but access is denied or not allowed.

What I have tried:

C#
string user = Environment.UserDomainName + "\\" + Environment.UserName;
            RegistrySecurity rs = new RegistrySecurity(); 
            rs.AddAccessRule(new RegistryAccessRule(user, RegistryRights.ReadKey | RegistryRights.WriteKey | RegistryRights.Delete, InheritanceFlags.None, PropagationFlags.None, AccessControlType.Allow));//To allow permission
RegistryKey rk =Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\OurSetting");
            rk.SetValue("setting1", "hello");
            rk.Close();
Posted
Updated 11-May-16 1:47am
v2
Comments
CHill60 3-May-16 8:34am    
Run as administrator
Member 11868117 3-May-16 9:04am    
If I create setup for this application then this error is occur or not when it run with adminstrator
CHill60 3-May-16 9:13am    
See Solution 1. This (your way) is a VERY bad way of using the Registry. The clue is in the key name ... "\\Microsoft\\" - you should have your own sub-folder for your software - what if Microsoft were to change the structure of this part of the registry (unlikely perhaps) - your software would cease to work.
HobbyProggy 3-May-16 8:35am    
Do you have administration rights? The Registry, and so is the path where Microsoft stores the keys, is locked (by privileges) for a reason you know?

Why won't you create a subkey in your own "folder" ?
Member 11868117 3-May-16 9:02am    
Thanks for reply actually I want to modify existing registry key.and add new key.

You cannot access very much of the registry without elevated permissions - which means UAC permission must be sought.

I'd strongly recommend that you don't use the registry at all, but store your data locally instead - access is restricted, and likely to become more restricted with time rather than less.

But: How to self-elevate an application to a high privilege level under UAC[^] may help in the (very) short term.
 
Share this answer
 
To access registry information or windows folder information, your user should have a administrative rights, OR in your EXE you can set the settings that run with administrative access only.
Right click on exe -> go to properties -> select Compatibility tab -> under privilege level -> check 'run this program as an administrator' checkbox

Hope it helps
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900