I\'ve recently written an application that impersonate user account, get a handle to CURRENT_USER registry key (using PInvoke \"LoadUserProfile\" to retrieve ProfileInfo.hPr
Wrote an Impersonation class I posted here to answer the same kind of question: Impersonate admin account to edit registry key not working (C#)
To write to the key, you just do:
string userName = "domain\user";
string password = "whatever";
string KEY_STR = "SomeSubKey\\ASubKeyToThat";
WindowsImpersonationContext adminContext = Impersonation.getWic(userName, password);
if (adminContext != null)
{
try
{
RegistryKey key = Registry.CurrentUser.OpenSubKey(KEY_STR, true);
key.SetValue("State", 0x60001);
}
catch (Exception ex)
{
Console.Out.WriteLine("\nUnable to set registry value:\n\t" + ex.Message);
Impersonation.endImpersonation();
adminContext.Undo();
}
finally
{
Impersonation.endImpersonation();
// The above line does this --
//if (tokenHandle != IntPtr.Zero) CloseHandle(tokenHandle);
adminContext.Undo();
}
}
No handles or other fancy functions, other than to get the WindowsImpersonationContext
needed. Didn't repost that part because it looks like you already know how to get the WIC.