Click to See Complete Forum and Search --> : Registry - writing to HKLM


Ka-lolek
September 1st, 2009, 08:02 AM
Hello,
i have problem cause when i try to write to hkey_local_machine, i get an exception with message
The parameter is incorrect.
the code looks like this:

objReg = Registry.LocalMachine;
RegistryKey obj = objReg.OpenSubKey(Key, true);

if (obj == null)
{
objReg.CreateSubKey(Key).SetValue(Value, "");
}
else
{
objReg.SetValue(Value, "");
}


i was looking for the solution in the internet, but i only found that the couse can be the lack of administrator privileges..
i added this part:

[assembly: RegistryPermissionAttribute(SecurityAction.RequestMinimum, ViewAndModify = "HKEY_LOCAL_MACHINE")]

With no result
do you know what cases this problem?
Cheers

monalin
September 1st, 2009, 09:51 AM
Use the LogonUser windows API to impersonate an administrator.

http://msdn.microsoft.com/en-us/library/system.security.principal.windowsimpersonationcontext.aspx

Ka-lolek
September 2nd, 2009, 12:49 AM
but there is one more problem..
my username is in administrators group but in spite of this when i open regedit and click add->key in HKEY_LOCAL_MACHINE, i get the message
Cannot create key: Error writing to the registry
so i think even if i impersonate an administrator it won't work, cause it doesn't work even through regedit...

edit
maybe i should add that i want to craete key directly in HKLM
e.g. HKLM\myKey

and i can create keys like: HKLM\Hardware\myKey

monalin
September 2nd, 2009, 09:35 AM
You can't Make a new key in that directory. Must choose one of the subdirectorys HARDWARE, SAM, SECURITY, SOFTWARE, or SYSTEM.

Ka-lolek
September 3rd, 2009, 12:10 AM
is there no possibility to create key directly in HKLM - at any way?

Ka-lolek
September 3rd, 2009, 11:58 PM
bump