C # registry operations

Source: Internet
Author: User
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 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.

Registrykey key = registry. localmachine;
Registrykey software = key. opensubkey ("Software \ test", true); // This item must already exist
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"); // 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 sub-items under the item and pass it to the predetermined Array
Foreach (string keyname in subkeynames) // traverses the entire array
{
If (keyname = "test") // you can specify 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 the predetermined Array

Foreach (string keyname in subkeynames)
{
If (keyname = "test") // judge the key value name
{
Hkml. Close ();
Return true;
}
}
Hkml. Close ();
Return false;
}

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.