Click here to Skip to main content
15,881,755 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
dear everyone i made a windows application using c#, in my application i have to write values in registry.. the problem exists in limited users (not administrator) accounts this error exception appear " Requested registry access is not allowed"

tried to solve this issue by 2 methods
method 1:
i put in my manifest file the below code
C#
<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <!-- UAC Manifest Options
            If you want to change the Windows User Account Control level replace the 
            requestedExecutionLevel node with one of the following.

        <requestedExecutionLevel  level="asInvoker" uiAccess="false" />
        <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
        <requestedExecutionLevel  level="highestAvailable" uiAccess="false" />

            If you want to utilize File and Registry Virtualization for backward 
            compatibility then delete the requestedExecutionLevel node.
        -->
        <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
      </requestedPrivileges>
    </security>
  </trustInfo>
</asmv1:assembly>



Method 2:
i tried to make the registry has full control for users so i can write into registry regardless of user type using this code
C#
string user = Environment.UserDomainName + "\\" + Environment.UserName;

RegistrySecurity rs = new RegistrySecurity();

// Allow the current user to read and delete the key.
//
rs.AddAccessRule(new RegistryAccessRule(user,
    RegistryRights.ReadKey | RegistryRights.WriteKey | RegistryRights.Delete,
    InheritanceFlags.None,
    PropagationFlags.None,
    AccessControlType.Allow));


it solve the error in many computers but there are many other computer has the same error.

i want to solve this error but i can not make it in all versions of windows.

please advice as soon as possible
Posted
Comments
johannesnestler 30-Oct-13 12:40pm    
do you really need to write to the registry while your app is running? what I think about is a scenario I once had, where in the end I found out that I should manipulate the registry only during installation (with a normal Windows Installer project), so high privileges are only needed while installing. Of course this was only a valid solution for my case (and resulted from the "wrong" approach to let the app write my needed value instead of the Setup)
John_Tadros 31-Oct-13 3:07am    
i have to write in registry during the run of the application cos other applications will take the value of my application to use it .. so it has to be in registry
John_Tadros 31-Oct-13 3:07am    
please advice

Well, we have no idea what part of the registry you're trying to write to. The full path to the requested key would help.

In any case, neither of these will work because your code is running AS THE USER that launched it. It has the same access permissions of the user.

Changing the requestedExecutionLevel will not work for you because if the user that launches your code doesn't have admin privileges, they won't get privileges that they don't already have.

Your second method will not work because you cannot grant yourself privileges that exceed what you already have.
 
Share this answer
 
Comments
John_Tadros 31-Oct-13 3:11am    
i am trying to access /HKEY_LOCAL_MACHINE/SOFTWARE/ could you please tell me any other idea to solve my problem because i have to write in registry during the run of the application cos other applications will take the value of my application written in registry to use it .. so i have to so9lve this problem as fast as possible
Dave Kreskowiak 31-Oct-13 7:06am    
Everything under HKEY_LOCAL_MACHINE is ReadOnly to normal users.

Use the same path but under HKEY_CURRENT_USER.
In addition to Dave Kreskowiak's Solution, you might add code that requests the user to run the program as local administrator.
Please note that setting a value (in an area where the user has privileges) does not need administrator privileges. See Understanding Windows File And Registry Permissions: Managing the Registry and Its Permissions[^].
Cheers
Andi
 
Share this answer
 
Comments
John_Tadros 31-Oct-13 4:19am    
location of check box : regedit --> any load hive at h user hive --> right click and select permissions --> click advance tab and then

at bootom two check box is there.and i want to select second one programmatically .

please guide me.
Andreas Gieriet 31-Oct-13 7:22am    
To check: Make a local user that is **not** part of the local administrator group, then login as that user and try to change that checkbox manually.
If you have the permission to do so, then you should be able to do it programmatically **if** there is an API that allows to do so.
If you have no permission as such a user, you cannot change that (for a good reason - otherwise, it would be a security hole like as if you could issue your own passport yourself).
You may consider make that change at installation time and require to be local administrator to install.
But: beware of changing the permissions - your programm should not need to do so! As a client of any program I would heavily **dislike** any program doing so. One could even think of taking regress to you since you might have opened up a security hole **on purpose**...
Cheers
Andi

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