02 March 2015

...get your hive on: accessing user registry value in an application using SecurityIdentifier


Problem
You want to update registry using an application but application encounters errors recorder HERE:


Solved
Switched from using the user hive hogging advapi32 API and now using System.Security.Principal.SecurityIdentifier to retrieve the SecurityIdentifier for the specific user whose registry I want to update and proceeding to use  Microsoft.Win32.RegistryKey to read and set the update keys.

The code looks something like this (sample code):

NTAccount ntAcct = new NTAccount(definedUserName);
SecurityIdentifier secId = (SecurityIdentifier)ntAcct.Translate(typeof(SecurityIdentifier));
String userSedId =  secId.ToString();

String userRegPath = userSedId + @"\Software\Google\Path...\Path..\Path";

RegistryKey regKey = Registry.Users.OpenSubKey(userRegPath, true);

userRegPath will look something like "S-9-9-99-1234567890-1234567890-123456789-123456\Software\Google\Path...\Path..\Path"


regKey will look something like "HKEY_USERS\S-9-9-99-1234567890-1234567890-123456789-123456\Software\ Google\Path...\Path..\Path"

Note to self: Stop playing with user hive already..