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.

Preparations:

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, which can be used to locate the branch at the beginning of the registry:

Classesroot, currentuser, users, localmachine, currentconfig

For example, registrykey = 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:

The createsubkey () method of registrykey is used to create a registry key. 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 mainly uses the opensubkey () method of registrykey. For example:

Registrykey key = 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 key = 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. 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 ("Software \ test", true); // This item must already exist in software. setvalue ("test", ""); // create a key value named "test" under HKEY_LOCAL_MACHINE \ SOFTWARE \ test with the value ". 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"); // in HKEY_LOCAL_MACHINE \ SOFTWARE \ test, change the default key value 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!

Iii. Check whether the registry key exists

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 subitem 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. Determine whether the key value exists

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 ;}

So far, C # has operated the registry. This article covers almost all the operations of C # language on the registry. As long as you carefully read this article, you must read and modify the Registry carefully ~~~ The reader has learned and my goal has been achieved ~~ :)

All the examples in this article are debugged in vs2008 + WINXP...

Please correct me for any errors or omissions. Thank you.

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.