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:
C # code
Using Microsoft. 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:
C # code
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:
C # code
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:
C # code
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:
C # code
RegistryKey key = Registry. LocalMachine;
Key. DeleteSubKey (softwaretest, true); // This method has no return value and can be called 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.
C # code
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:
C # code
Software. SetValue (test, 0, RegistryValueKind. DWord); // binary information
Ey. Close ();
2: Read:
C # code
String info =;
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:
C # code
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:
C # code
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
C # code
Private bool IsRegeditItemExist ()
{
String [] subkeyNames;
RegistryKey hkml = Registry. LocalMachine;
RegistryKey software = hkml. OpenSubKey (SOFTWARE );
RegistryKey software = hkml. OpenSubKey (SOFTWARE, true );
SubkeyNames = software. GetSubKeyNames ();
// Obtain the sequence of names of all sub-items under the item and pass it to the predetermined Array
Foreach (string keyName in subkeyNames) traverses the entire array
{
If (keyName = test) determines the name of the subitem
{
Hkml. Close ();
Return true;
}
}
Hkml. Close ();
Return false;
}
4. Determine whether the key value exists
C # code
Private bool IsRegeditKeyExit ()
{
String [] subkeyNames;
RegistryKey hkml = Registry. LocalMachine;
RegistryKey software = hkml. OpenSubKey (SOFTWAREtest );
RegistryKey software = hkml. OpenSubKey (SOFTWAREtest, true );
SubkeyNames = software. GetValueNames ();
// Obtain the sequence of names of all key values under the item and pass it to the predetermined Array
Foreach (string keyName in subkeyNames)
{
If (keyName = test) judge the key value name
{
Hkml. Close ();
Return true;
}
}
Hkml. Close ();
Return false;
}