C # Registry operations, create, delete, modify, determine whether a node exists

Source: Internet
Author: User

Using the. NET Managed language C # Operations registry, the main contents include: Creation of registry keys, opening and deletion, creation of key values (setting values, modifications), reading and

Deletes, determines whether the registry key exists, and determines whether the key value exists.

Preparatory work:
1: In order to manipulate the registry, we must introduce the necessary namespaces:

    1. Using Microsoft.Win32;


There are many registry related classes in this namespace, enough for us to use ~ ~
2: The namespace provides a class: RegistryKey uses it to navigate to the branch at the beginning of the registry:
Classesroot,currentuser,users,localmachine,currentconfig
Such as:

    1. RegistryKey key = Registry.localmachine;


3: In the process of operation involves sub-branches, to be used in depth, a single will error!
4: Last to invoke Close () of the RegistryKey object to turn off modification of the registry ~ ~ ~
5: Please note that our example below is under the LocalMachine branch.

One: Creation of registry keys, opening and deletion
1: Create:
Creating a registry key uses the RegistryKey CreateSubKey () method. Such as:

    1. RegistryKey key = Registry.localmachine;
    2. RegistryKey software = key. CreateSubKey (Softwaretest);


Create a new registry key named Test under Hkey_local_machinesoftware. It does not affect if it already exists!

2: Open:
Opening a registry key uses the RegistryKey OpenSubKey () method. Such as:

    1. RegistryKey key = Registry.localmachine;
    2. RegistryKey software = key.  OpenSubKey (Softwaretest,true);


Note that the method can also have a Boolean parameter followed by true to indicate that it can be written.
Note that if the registry key does not exist, this call to this method throws an exception

3: Delete:
Deleting a registry key uses the RegistryKey DeleteSubKey () method. Such as:

    1. RegistryKey key = Registry.localmachine;
    2. Key. DeleteSubKey (Softwaretest,true); //The method has no return value and can be called directly
    3. Key. Close ();


Note that if the registry key does not exist, this call to this method throws an exception

Two: Key value creation (set value, modify), read and delete
1: Create (set value, modify):
The creation and modification of key values is mainly used in the RegistryKey SetValue () method.

    1. RegistryKey key = Registry.localmachine;
    2. RegistryKey software = key. OpenSubKey (Softwaretest,true); The item must already exist
    3. Software. SetValue (Test, park);


Under Hkey_local_machinesoftwaretest, create a key value named "Test" with a value of "garden". If the key value originally existed,

Replaces the original key value, or creates the key if it does not exist.
Note: SetValue () also has a third parameter, mainly for setting the type of the key value, such as: string, Binary, DWORD, etc. ~ ~ The default is the string.

Such as:

    1. Software. SetValue (test, 0, Registryvaluekind.dword); //binary information
    2. ey. Close ();



2: Read:

    1. string info =;
    2. RegistryKey Key;
    3. Key = Registry.localmachine;
    4. Myreg = Key.opensubkey (softwaretest);
    5. Myreg = Key.opensubkey (Softwaretest,true);
    6. info = Myreg. GetValue (Test). ToString ();
    7. Myreg. Close ();


The info result is: Park

3: Delete:

    1. RegistryKey Delkey = Registry.LocalMachine.OpenSubKey (Softwaretest, true);
    2. Delkey.deletevalue (test);
    3. Delkey.close ();



The careful reader may find the second example in which the OpenSubKey () method parameter differs from other examples.
If you want to modify the key value, including create, set, delete the key value and so on to add a Boolean parameter after the method, set to True, indicating writable can be changed;

Just read the key value can not add, at this time can write off, you can no longer write the value (of course, you want to add also can true)!
Readers also mentioned the problem of reading and writing the default key values, mainly in the settings, read the method of the key name is the default key value operation.
Such as:

    1. Software. SetValue (, park);


In Hkey_local_machinesoftwaretest, modify the value of the default key value to "garden". Read similar!
In addition, the default key value can not be deleted, so do not use the DeleteValue () method to delete, will throw an exception!


Three: Determine if the registry key exists

  1. Private bool Isregedititemexist ()
  2. {
  3. String[] Subkeynames;
  4. RegistryKey hkml = registry.localmachine;
  5. RegistryKey software = hkml. OpenSubKey (software);
  6. RegistryKey software = hkml.  OpenSubKey (software, true);
  7. Subkeynames = software. Getsubkeynames ();
  8. Gets the sequence of the names of all the subkeys under the key, and passes them to the predetermined array
  9. foreach (string keyName in subkeynames) traverses the entire array
  10. {
  11. if (keyName = = test) Determine the name of the subkey
  12. {
  13. Hkml. Close ();
  14. return true;
  15. }
  16. }
  17. Hkml. Close ();
  18. return false;
  19. }



Four: Determine if the key value exists

    1. Private bool Isregeditkeyexit ()
    2. {
    3. String[] Subkeynames;
    4. RegistryKey hkml = registry.localmachine;
    5. RegistryKey software = hkml. OpenSubKey (Softwaretest);
    6. RegistryKey software = hkml.  OpenSubKey (Softwaretest, true);
    7. Subkeynames = software. GetValueNames ();
    8. Gets the sequence of names of all the key values under that key, and passes them to the predetermined array
    9. foreach (string keyName in subkeynames)
    10. {
    11. if (keyName = = test) Determine the name of the key value
    12. {
    13. Hkml. Close ();
    14. return true;
    15. }
    16. }
    17. Hkml. Close ();
    18. return false;
    19. }

C # Registry operations, create, delete, modify, determine whether a node exists

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.