C # Full introduction to registry operations

Source: Internet
Author: User

I believe everyone is familiar with the Registry. Enter "Regedit" in the operation to open the Registry Editor. This is a very important thing for Windows systems. It is also a frequent place for viruses, for example, viruses and malware often write their own boot key values in the startup items of the Registry to achieve self-startup. Some viruses also modify the Registry to hijack anti-virus software, this is the first step to damage the system. At the same time, most software (software serial numbers and information) and hardware information, system information, security mode, and other settings are stored here, therefore, the System Health depends largely on the registry health. As a programming developer, we need to understand the registry and learn how to operate the registry. Here we will use it. C #, a managed language under. net, operates the registry. The main content includes: create a registry key, open and delete it, and create a key value (set value, modify ), read and delete, judge whether the registry key exists, and determine whether the key value exists.

Preparation: 1: To operate the registry, we must introduce the necessary namespace: using Microsoft. win32; this namespace contains many registry-related classes, which is enough for us to use ~~ 2: The namespace provides a class: registrykey. We can use it to locate the branch starting with the Registry: classesroot, currentuser, users, localmachine, currentconfig, for example, registrykey key = registry. localmachine; 3: subbranches are involved in the operation. Use \ for in-depth analysis. An error is reported for a single! 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: create a registry entry using the createsubkey () method of registrykey. For example, registrykey key = registry. localmachine; registrykey software = key. createsubkey ("Software \ test"); // create a registry key named test under HKEY_LOCAL_MACHINE \ Software. If it already exists, it will not be affected!

2: open: Open the registry key to use the opensubkey () method of registrykey. For example, registrykey = registry. localmachine; registrykey software = key. opensubkey ("Software \ test", true); // note that this method can 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 = registry. localmachine; key. deletesubkey ("Software \ test", true); // This method has no return value. You can directly call the key. close (); note that if the registry key does not exist, this method will throw an exception.

II. Key Value Creation (set value, modify), read and delete 1: Create (set value, modify ): key value creation and modification operations mainly use the setvalue () method of registrykey = registry. localmachine; registrykey software = key. opensubkey ("Software \ test", true); // This item must already exist in software. setvalue ("test", "blog ");

// Create a key value named "test" under HKEY_LOCAL_MACHINE \ SOFTWARE \ test with the value of "blog Garden. If the key value already exists, it is changed to replace the original key value. If it does not exist, it is created. // Note: setvalue () has a third parameter, which is mainly used to set the type of the key value, such as string, binary, DWORD, and so on ~~ The default value is a string. For example: // software. setvalue ("test", "0", registryvaluekind. DWORD); // binary information key. Close ();

2: Read: String info = ""; registrykey key; Key = registry. localmachine; myreg = key. opensubkey ("Software \ test"); // myreg = key. opensubkey ("Software \ test", true); Info = myreg. getvalue ("test "). tostring (); myreg. close (); Info: blog

3: delete: registrykey delkey = registry. localmachine. opensubkey ("Software \ test", 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 the key value, including creating, setting, and deleting the 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 you only want to read the key value, you can not add 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 ("", "blog"); // modify the default key value in HKEY_LOCAL_MACHINE \ SOFTWARE \ test to "blog ". Read similar! In addition, the default key value cannot be deleted, so do not delete it using the deletevalue () method. An exception is thrown!

 

3. Check whether the registry key has 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 foreach (string keyname in subkeynames) in the predetermined array) // traverse the entire array {If (keyname = "test") // judge the subitem name {

Hkml. Close (); Return true ;}} hkml. Close (); Return false ;}

4. Check whether the key value has private bool isregeditkeyexit () {string [] subkeynames; registrykey hkml = registry. localmachine; registrykey software = hkml. opensubkey ("Software \ test"); // registrykey software = hkml. opensubkey ("Software \ test", true); subkeynames = software. getvaluenames (); // obtain the sequence of names of all key values under the item and pass it to foreach (string keyname in subkeynames) in the predetermined array) {If (keyname = "test") // judge the key value name {hkml. close (); Return true ;}} hkml. close (); Return false ;}

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.