C # System Application of the registration form using specific explanations

Source: Internet
Author: User

when doing the project, we sometimes encounter the operation of the register, for example, we need to obtain the Internet Explorer address bar information, access to the "My Computer" Address bar input directory information, USB recent use information. The Register entry is the basic organizational unit of the Register, which includes child table entries and value entries. In short, The registration form is equivalent to the catalogue in the booklet. They store information about the computer installer, such as color settings, screen size, history, and so on.
For example: When we play Warcraft or DotA, we always need to open the "execute", "regedit" to open the registration form, in Hkey_current_user->software->blizzard Entertainment->warcraft Iii->video Changes the reswidth (screen width) and resheight (screen height). Make their screen resolution consistent with the game full screen display.
The following is a detailed explanation of the use of the register.
I. Structure of the Register
The registration Form (registry) is an important database in Windows systems that stores information about applications, users, and systems. The structure of the register is like a tree. The tree's top node (hive) cannot be added, altered, or deleted.
For example, you see a top-level node for the Windows registry:

to manipulate the register in C #, you need to reference the namespace:using Microsoft.Win32.
RegistryKey classrepresents the top-level node in the register, which is the encapsulation of the Register.
Registry Classprovides an RegistryKey object that represents the root item in the Windows registry, and provides a static method for access to the item/value. The properties of the top-level node (hive) of the registry object are often used, as seen in the following table:

Registry Property Register top node Description
Classessroot HKey_Classes_Root Define the document type \ Class and the information associated with the type and the configuration data for the COM component
Currentconfig Hkey_current_config Includes configuration information about non-user-specific hardware
CurrentUser HKey_Current_User Includes configuration information for the user who is currently logged on to Windows
LocalMachine HKEY_Local_Machine Includes computer-related configuration information, whether or not the user is logged on
Users Hkey_users Includes information about the default User Configuration

        The data types that are frequently used in the register are:
        reg_ The main type of SZ string data used to store fixed-length strings or other short text values. We typically use only such data types in actual programs, assuming that you want to save the Boolean value, it is represented as 0 or 1.
        reg_binary is used to store binary data.
        REG_EXPAND_SZ An extensible string value that can hold system variables that are parsed at execution time.
        REG_MULTI_SZ saves multiple text strings in an array format, and each string "element" ends with a null character.
two. Registration Form regular usage
           1. Create a register key
        
first determine which top node to create the key under. Then call the CreateSubKey () method to create the.

Public RegistryKey CreateSubKey (String subkey   //The name or path of the subkey to be created or opened)

For example, create keys Eastmount and Hkey_current_user\software\eastmount\test2 under HKEY_CURRENT_USER. We most often use the Software This key to create the program product key, save some program configuration in the Register. Assuming that there is no Eastmount key in the software, the key and its subkeys are created first. Assuming there is no rewriting.

Create key//Create Eastmount key under HKEY_CURRENT_USER RegistryKey test1 = Registry.CurrentUser.CreateSubKey ("Eastmount");//Create key structure Hkey_current_user\software\eastmount\test2registrykey test2 = Registry.CurrentUser.CreateSubKey (@ "software\ Eastmount\test2 ");

Note: A backslash in a C # string is an escape character, preceded by a prefix @, that tells the string to be processed in literal value without escaping [email protected] "software\eastmount\test2" = "Software\\eastmount\\test2 ".
the results of the execution are as follows:

2. Turn on sub-keys

Public RegistryKey OpenSubKey (string name,     //subkey name to open or path bool writable    //assume permission to write access =true)

3. Delete the key of the registration form
There are two ways to delete a key from the Register: DeleteSubKey () and Deletesubkeytree (). DeleteSubKey () removes the key and all its values that do not contain subkeys, Deletesubkeytree () not only deletes the key and its value, but also removes all subkeys under the key (using this method with caution).

function prototype public void DeleteSubKey (String subkey    //subkey name to delete)//delete Hkey_current_ User under Create Eastmount key Registry.CurrentUser.DeleteSubKey ("Eastmount");// Delete the Created subkey Test2Registry.CurrentUser.DeleteSubKey (@ "Software\eastmount\test2");

Assuming that the key does not exist, there will be an exception error prompt, preferably using try: Catch.. Exception handling. It only deletes software\eastmount test2 and retains the Eastmount item. Results for example as seen in:

Note: Assume that you use Registry.CurrentUser.DeleteSubKey (@ "Software\eastmount"); it will prompt the error "The register has subkeys, and this method does not support recursive removal." Suppose you use Registry.CurrentUser.DeleteSubKeyTree (@ "Software\eastmount") to delete Eastmount and subkey test2.
4. Get the key value
       
Registry.getvalue () retrieves the value associated with the specified name in the specified annotation list item. If the subkey specified by KeyName does not exist, NULL is returned, otherwise the value associated with ValueName is returned; If ValueName is not found, it returns defaultvalue.
public static Object GetValue (String keyName,//        The full path of the key that starts with the top node of the registry string valueName,      //name/value name of the object DefaultValue    //value returned when ValueName does not exist)

For example, get the path key value in Hkey_current_user\environment.

Gets environment in path string strpath;strpath = (string) registry.getvalue (@ "hkey_current_user\environment",    "path" , "Return This default if path does not exist"); MessageBox.Show (strpath);


The RegistryKey.GetValue method can retrieve the value associated with the specified name. function prototype public object GetValue (string name). The number of references name indicates the key. As in the program I get the information entered in the computer's address bar.

Define a table top node namespace Microsoft.Win32Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey    ("Software\\microsoft\\windows\\currentversion\\explorer\\typedpaths", true);//infer if the key exists if (key! = null) {    / /retrieves all the value names that include this association (URL1 url2 url3    string[] names = key. GetValueNames ();    foreach (string str in names)    {        //Gets the value LISTBOX1.ITEMS.ADD (key) associated with the URL        . GetValue (str). ToString ());        num++;    }    Show get total number    of files This.textBox1.Text = Num + "Files";}

The result of the execution is, for example, seen. In fact, there are a lot of things, USB usage information, browser usage information, software path, and so on.

5. Create and set key values
The Registry.setvalue () method sets the specified name/value pair for the specified annotation list item, assuming that the specified item does not exist.

function prototype public static void SetValue (    string keyName,/        /full path to the key that starts with the top node of the registry string valueName,      //Name/ The name of the value pair object value           //value to store)//Set key value Version=1.25registry.setvalue (@ "Hkey_current_user\software\yoursoftware", " Version "," 1.25 ");

Note: Each time you call the SetValue method, it opens a list entry, sets its value, and then closes the item. It is recommended that you use the RegistryKey.SetValue method if you require large volume changes. Public Object SetValue ( string Name,object value).
PS: Give a simple example by setting My Computer-{20d04fe0-3aea-1069-a2d8-08002b30309d} to 1, you can hide the desktop My Computer icon. Changes to 0 can be displayed, do you know to add the directory ". {20d04fe0-3aea-1069-a2d8-08002b30309d} "has become" My Computer "and can be used to hide files.

RegistryKey rgk = Registry.CurrentUser.CreateSubKey (@ "Software\microsoft\windows\currentversion\explorer\ Hidedesktopicons\newstartpanel "); Rgk.setvalue (" {20d04fe0-3aea-1069-a2d8-08002b30309d} ", 1);

finally hope that the article is helpful to everyone, the article is mainly their own this period of time on the C # Call register information Summary, on the one hand to tell the basic knowledge of the Register table, there is also a part of the code in combination with the picture format to tell the simple application of the Register. At the same time, suppose there are any wordy or deficient articles Please also Haihan.
articles on your project, MSDN, and using C # Operations Brochure
(by:eastmount 2014-5-11 Night 9:30 original csdnhttp://blog.csdn.net/eastmount/)

C # System Application of the registration form using specific explanations

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.