Use C #, a managed language under. NET, to operate the registry. The main content includes: create a registry key, open and delete it, create a key value (set value, modify), read and
Delete or judge whether the registry key exists or whether the key value exists.
Preparations:
1: To operate the registry, we must introduce the necessary namespace:
UsingMicrosoft. Win32;
This namespace contains many registry-related classes, which is enough for us to use ~~
2: The namespace provides a class: RegistryKey, which can be used to locate the branch at the beginning of the registry:
ClassesRoot, CurrentUser, Users, LocalMachine, CurrentConfig
For example:
RegistryKey key = Registry. LocalMachine;
3: subbranches are involved in the operation process, which must be in-depth. An error will be reported for each Subbranch!
4: Finally, you need to call Close () of the RegistryKey object to Close the modification to the Registry ~~~
5: The following examples are all under the LocalMachine branch. Please note.
1. Create, open, and delete a registry entry
1: Create:
The CreateSubKey () method of RegistryKey is used to create a registry key. For example:
RegistryKey key = Registry. LocalMachine;
RegistryKey software = key. CreateSubKey (softwaretest );
Create a registry key named test under HKEY_LOCAL_MACHINESOFTWARE. If it already exists, it will not be affected!
2: open:
Open the registry key mainly uses the OpenSubKey () method of RegistryKey. For example:
RegistryKey key = Registry. LocalMachine;
RegistryKey software = key. OpenSubKey (softwaretest,True);
Note that this method can also be followed by a Boolean parameter. true indicates that data can be written.
Note: If the registry key does not exist, this method will throw an exception.
3: delete:
To delete a registry key, you must use the DeleteSubKey () method of RegistryKey. For example:
- RegistryKey key = Registry. LocalMachine;
- Key. DeleteSubKey (softwaretest,True); // This method has no return value. You can call it directly.
- Key. Close ();
Note: If the registry key does not exist, this method will throw an exception.
Ii. Create (set and modify), read, and delete key values
1: Create (set value, modify ):
The SetValue () method of RegistryKey is used for key value creation and modification.
- RegistryKey key = Registry. LocalMachine;
- RegistryKey software = key. OpenSubKey (softwaretest,True); This item must already exist
- Software. SetValue (test, Garden );
Create a key value named "test" and its value is "Garden" under HKEY_LOCAL_MACHINESOFTWAREtest. If the key already exists,
The original key value will be modified and replaced. If it does not exist, the key value will be created.
Note: SetValue () has a third parameter, mainly used to set the key value type, such as string, binary, Dword, and so on ~~ The default value is a string.
For example:
- Software. SetValue (test, 0, RegistryValueKind. DWord); // binary information
- Ey. Close ();
2: Read:
- StringInfo =;
- RegistryKey Key;
- Key = Registry. LocalMachine;
- Myreg = Key. OpenSubKey (softwaretest );
- Myreg = Key. OpenSubKey (softwaretest,True);
- Info = myreg. GetValue (test). ToString ();
- Myreg. Close ();
Info: Garden
3: delete:
- RegistryKey delKey = Registry. LocalMachine. OpenSubKey (Softwaretest,True);
- DelKey. DeleteValue (test );
- DelKey. Close ();
Careful readers may find that the OpenSubKey () method parameters in the second example are different from those in other examples.
If you want to modify a key value, including creating, setting, or deleting a key value, you must add a Boolean parameter after the method and set it to true, which indicates that the key value can be written or modified. If only
You can only read the key value without adding it. In this case, you can disable it. You can no longer write the value to it (of course, you can add it to true )!
Readers also mentioned the problem of reading and writing the default key value. Setting the key name blank in the setting and reading methods is the operation of the default key value.
For example:
Software. SetValue (, Garden );
In HKEY_LOCAL_MACHINESOFTWAREtest, change the default key value to "Garden ". Read similar!
In addition, the default key value cannot be deleted, so do not delete it using the DeleteValue () method. An exception is thrown!
Iii. Check whether the registry key exists