writing new value to registry key

P

Piotrekk

Hi
I have accessed registry key where system path is stored:

RegistryKey myKey =
Registry.LocalMachine.OpenSubKey("System\\CurrentControlSet\\Control\\Sessi­on

Manager\\Environment");


Then i'am trying to change the value:


if (myKey != null)
{
object val = myKey.GetValue("Path");
RegistryValueKind rvk = myKey.GetValueKind("Path");



val = dir + ";" + val;


myKey.DeleteValue("Path");
myKey.SetValue("Path", val);


return;
}


OpenSubKey returns key read only. I dunno how to set access to this
regkey such that is would be possible to write to it.
How to change privilages to force program to write new val to the reg?
Thanks for any sugestions
Piotr Kolodziej
 
C

Cubaman

Hello:
Yoy have and overloaded method for this, just passing a boolean
indicating if you want write acces to the key. Here is an example:

try {
RegistryKey regKey = Registry.LocalMachine;
regKey =
regKey.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run",
true); // This last bool values specify write acces to registry
regKey.SetValue("SomeValue);
regKey.Close();
}
catch (SecurityException) {
throw new InstallException("You need administrator
rights to install this program.");
}

Happy coding!!

Oscar Acosta
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top