Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to edit the registry keys of a specific user programatically?

I want to change a few settings of a Windows user that I created in my application. If I understand correctly, his "HKEY_CURRENT_USER" values will be under HKEY_USERS/<sid>/.... Is this correct? How can I get the sid of the user, if I know the user name and the domain?

Edit: How can I correctly edit the HKCU keys of that user, if I have the sid already?

like image 616
Tamás Szelei Avatar asked Apr 02 '12 14:04

Tamás Szelei


People also ask

How can I edit a protected registry key?

In Registry Editor, right-click the key that you can't edit (or the key that contains the value you can't edit) and then choose “Permissions” from the context menu. In the Permissions window that appears, click the “Advanced” button. Next, you're going to take ownership of the Registry key.

Is Hkey_current_user different for each user?

The HKEY_CURRENT_USER registry hive is specific to each user, so in order to modify this for another user, we first need to identify where that information is stored.


1 Answers

I have a program that does exactly that. Here is the relevant part of the code:

NTAccount ntuser = new NTAccount(strUser);
SecurityIdentifier sID = (SecurityIdentifier) ntuser.Translate(typeof(SecurityIdentifier));
strSID = sID.ToString();

You will need to import two namespaces:

using System.DirectoryServices;
using System.Security.Principal;

Hope this helps.

Then use Registry.Users.SetValue with SID string\path to set the registry value.

This might not work as intended if you are editing a logged-off profile, especially a roaming profile.

like image 171
deutschZuid Avatar answered Sep 27 '22 21:09

deutschZuid