C # registry operation, create, delete, modify, and determine whether a node exists,

Source: Internet
Author: User

C # registry operation, create, delete, modify, and determine whether a node exists,

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:

  1. 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:

  1. 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:

  1. RegistryKey key = Registry. LocalMachine;
  2. 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:

  1. RegistryKey key = Registry. LocalMachine;
  2. 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:

  1. RegistryKey key = Registry. LocalMachine;
  2. Key. DeleteSubKey (softwaretest, true); // This method has no return value and can be called directly.
  3. 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.

  1. RegistryKey key = Registry. LocalMachine;
  2. RegistryKey software = key. OpenSubKey (softwaretest, true); this item must already exist
  3. 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:

  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 ();


Info: Garden

3: delete:

  1. RegistryKey delKey = Registry. LocalMachine. OpenSubKey (Softwaretest, true );
  2. DelKey. DeleteValue (test );
  3. 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:

  1. 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

  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. // Obtain the sequence of names of all sub-items under the item and pass it to the predetermined Array
  9. Foreach (string keyName in subkeyNames) traverses the entire array
  10. {
  11. If (keyName = test) determines the name of the subitem
  12. {
  13. Hkml. Close ();
  14. Return true;
  15. }
  16. }
  17. Hkml. Close ();
  18. Return false;
  19. }



4. Determine whether 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. // Obtain the sequence of names of all key values under the item and pass it to the predetermined Array
    9. Foreach (string keyName in subkeyNames)
    10. {
    11. If (keyName = test) judge the key value name
    12. {
    13. Hkml. Close ();
    14. Return true;
    15. }
    16. }
    17. Hkml. Close ();
    18. Return false;
    19. }

Related Article

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.