First look at a wrong Code:
String subkeyname = @ "SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run \"; // subkey name
String valuename = @ "app name"; // name of the more specific key that will hold the value, "" means (default)
Try
...{
Registrykey Reg = registry. localmachine. opensubkey (subkeyname );
If (Reg! = NULL)
...{
Reg. deletevalue (valuename );
Reg. Close ();
}
}
Catch (exception ex)
...{
MessageBox. Show (this, Ex. tostring ());
}
Run this code and you will receive the following exceptions:
System. unauthorizedaccessexception
The reason is simple:
Registrykey. opensubkey (string) retrieves subitems in read-only mode
Public registrykey opensubkey (string name, bool writable) writable is set to true if the write access permission of the item is required.
We need to take the second parameter to indicate that we can open it in writable mode.