C # simple registry operations

Source: Internet
Author: User
1. Registry Introduction

Registry viewing toolRegedit.exe. The highest level mainly includes five keys.

 

There are also two hidden ones, which are generally not commonly used.

2. Registry operations

The. Net Operation registry mainly uses two classes, the namespace is Microsoft. Win32.

  • Registrykey: Key-specific operations, including three read-only attributes and some column methods, respectively add, delete, and set the sub-keys and values.
  • Registry:It mainly includes seven read-only attributes, corresponding to the seven top-level keys that may exist.

3. Registry instance

The example is simple: select a color from the drop-down box as the background color of the form. The settings are automatically saved when the window is closed.

2. Page Layout: Create a Windows application program and add a ComboBox control to the form;

2. initialize the color drop-down box: here we will useReflectionTo obtain the standard color list.

 

Private void displaycolor ()
{
Type colortype = typeof (color );
Propertyinfo [] info = colortype. getproperties ();
Foreach (propertyinfo PI in info)
{
If (PI. propertytype = typeof (color) & Pi. Name! = "Transparent ")
{
Combobox1.items. Add (PI. Name );
}
}
}

Note:: Because the background color of the form cannot be set to transparent, you must exclude "Transparent ".

2. The format color changes accordingly. Add an event to the ComboBox control:

 

Selectedindexchanged

Private void combobox#selectedindexchanged (Object sender, eventargs E)
{
This. backcolor = color. fromname (combobox1.selecteditem. tostring ());
}

 

2. When the form is closed, the background color is saved;

 

1 private void savecolor ()
2 {
3 try
4 {
5 registrykey colorkey = registry. currentuser. createsubkey (@ "SOFTWARE \ Cathy \ colors ");
6 colorkey. setvalue ("backcolor", combox1. selecteditem. tostring ());
7 colorkey. Close ();
8}
9 catch (exception ex)
10 {
11 MessageBox. Show ("loading failed" + ex. Message. tostring ());
12}
13}

 

Then, we override the form's dispose () event and call the above method when releasing the form object.

 

 

Dispose

1 protected override void dispose (bool disposing)
2 {........
7 base. Dispose (disposing );
8 If (combobox1.selectedindex! =-1)
9 savecolor ();
10}

 

Run the program, as shown in:

 

Open the registry and find the corresponding key value:

 

2. When the form is opened, the background color set by the user is loaded.

 

1 private void getcolor ()
2 {
3 try
4 {
5 registrykey colorkey = registry. currentuser. opensubkey (@ "SOFTWARE \ Cathy \ colors ");
6 string colorname = (string) colorkey. getvalue ("backcolor ");
7 color = color. fromname (colorname );
8 This. backcolor = color;
9 combobox1.selecteditem = colorname;
10 colorkey. Close ();
11}
12 catch (exception ex)
13 {
14 MessageBox. Show ("failed to get" + ex. Message. tostring ());
15}
16}

 

Then you can call it in the form constructor.

Re-run the program and load it successfully.

 

 

 

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.