C # support for registry programming

Source: Internet
Author: User
Tags win32

Registry basics See this site article: Registry Learning Tutorial

When an application is installed, it is often necessary to use the registry to register the name of the application, the running path, the user's configuration information for the application, and so on. Many applications often need access to the registry at run time.

. NET Framework structure provides two classes for registry operations within the Microsoft.Win32 namespace:

Registry and RegistryKey. These two classes are sealed and are not allowed to be inherited by other classes.

The Registry class provides 7 public static domains representing the 7 basic primary keys in the Windows registry, which are:

Registry.classroot, corresponding to the HKEY_CLASSES_ROOT primary key.

Registry.currentuser, corresponding to the HKEY_CURRENT_USER primary key.

Registry.localmachine, corresponding to the HKEY_LOCAL_MACHINE primary key.

Registry.users, corresponding to the HKEY_USERS primary key.

Registry.currentconfig, corresponding to the Hkey_current_config primary key.

REGISTRY.DYNDTA, corresponding to the Hkey_dyn_data primary key.

Registry.PerformanceData, corresponding to the Hkey_performance_data primary key.

The basic operation of the Windows Registry is encapsulated in the RegistryKey class. The operation on the registry must conform to the permissions provided by the system, or the specified action cannot be completed, and the program throws an exception.

To create a child key

The prototype of the member method that creates the subkey is:

Public RegistryKey CreateSubKey (string subkey);

Where the parameter subkey represents the name of the subkey or the full path name of the subkey to be created. If the creation succeeds, the return value is the subkey that was created, otherwise null.

To open a child key

The member method for opening a subkey is a prototype:

Public RegistryKey OpenSubKey (string name);

Public RegistryKey OpenSubKey (string name,bool writable);

The name parameter represents the subkey or full path name to be opened, and the writable parameter indicates whether the primary key being opened can be modified.

The first method is read-only by default for open subkeys, and if you want to write to an open primary key, use the second method and set the Writalbe parameter value to True.

The namespace Microsoft.Win32 also provides us with another way to open the registry on the remote machine to operate. The method prototype is:

public static RegistryKey OpenRemoteBaseKey (registryhive hkey,string machinename);

Delete a child key

The DeleteSubKey method is used to delete the specified subkey, and the method prototype is:

public void DeleteSubKey (string subkey);

When you use the DeleteSubKey method, the deletion fails and an exception is returned if the subkey is also included in the child key. You can use the Deletesubkeytree method if you want to delete the subkeys completely, that is, delete the subkeys and all subkeys below the subkeys. The prototype of the method is:

public void Deletesubkeytree (string subkey);

Reading key values

The method for reading the key is:

public Object GetValue (string name);

public Object GetValue (string Name,object defaultvalue);

The name parameter represents the names of the keys, and the return type is an object type. If the key specified in the method does not exist, the method returns a null. When using the GetValue method, you do not have to care whether the value type of the key is a string, binary, or DWORD type, as long as the correct return type is used. For example, if we want to read a key value of a string type, the code can write this:

String S_value=key. GetValue ("Type");

Where key represents a primary key.

If you are unsure whether the key value exists and you do not want a null return value, use the second method GetValue (string Name,object defaultvalue), where the parameter defaultvalue represents the default return value. If the read fails, the return value is the value passed to the parameter defaultvalue.

Set Key values

The method prototype for setting the key value is:

public void SetValue (string name,object value);

Similarly, when we use this method to modify a key value, we do not have to bother to distinguish which value type to pass, the method will identify which type, and assign the value of the corresponding type to the specified key.

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.