1 using Microsoft. Win32;
2 using system. diagnostics;
3 private void access_registry ()
4 {
5 // create a new key under hkey_local_machinesoftware and name it mcbinc
6 registrykey key = registry. localmachine. opensubkey ("software", true );
7 // Add a subkey
8 registrykey newkey = key. createsubkey ("mcbinc ");
9
10 // set the value of this subkey
11 newkey. setvalue ("mcbinc", "net developer ");
12
13 // obtain data from other places in the Registry
14
15 // find your CPU
16 registrykey pregkey = registry. localmachine;
17 pregkey = pregkey. opensubkey ("hardwaredescriptionsystemcentralprocessor ");
18 object val = pregkey. getvalue ("vendoridentifier ");
19 Debug. writeline ("the central processor of this machine is:" + val );
20 // Delete the key value
21 registrykey delkey = registry. localmachine. opensubkey ("software", true );
22 delkey. deletesubkey ("mcbinc ");
23}
24
To write the registry:
An error is reported when the registry value is written.
System. unauthorizedaccessexception: cannot be written to the registry key
You can set 2nd parameters to true for the opensubkey () method, and 2nd parameters to true to indicate readable and writable registries.