Home Backend Development C#.Net Tutorial C# registry operation implementation code

C# registry operation implementation code

Dec 22, 2016 pm 01:41 PM

The second-level directory contains five predefined primary keys: HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_USERS, and HKEY_CURRENT_CONFIG.

Let’s explain the functions of these 5 classes respectively

  HKEY_CLASSES_ROOT This primary key contains the file extension and application related information, as well as the information used by Window Shell and OLE to store the registry. The subkeys under this primary key determine how to display this type of files and their icons in WINDOWS. This primary key is mapped from HKEY_LCCAL_MACHINESOFTWAREClasses.
HKEY_CURRENT_USER This primary key contains information about the current user such as user window information, desktop settings, etc.

HKEY_LOCAL_MACHINE primary key contains the installation and configuration information of computer software and hardware. This information is available to all users.
 HKEY_USERS primary key records the current user’s setting information. Every time the user logs in to the system, a primary key will be generated under this primary key. A subkey that is the same as the user's login name. This subkey saves the current user's desktop settings, background bitmap, shortcut keys, fonts and other information. Generally, applications do not directly access the primary key, but access it through the primary key HKEY_CURRENT_USER.

 HKEY_CURRENT_CONFIG This primary key saves the current hardware configuration information of the computer. These configurations can be changed according to the type of network currently connected or the installation of hardware driver software.

 C# also supports editing of the registry. The .NET framework provides two classes in the Microsoft.Win32 namespace to operate the registry: Registry and RegistryKey. Both classes are sealed classes and are not allowed to be inherited. Below we introduce these two classes respectively.

 The Registry class provides 7 public static fields, representing 7 basic primary keys respectively (two of which are not in the XP system and will not be introduced here): Registry.ClassesRoot, Registry.CurrentUser, Registry.LocalMachine , Registry.Users, Registry.CurrentConfig. I think you will know which keys they correspond to at a glance.

 The RegistryKey class provides methods for operating the registry. It should be noted that operating the registry must comply with system permissions, otherwise an error will be thrown.

 Now let’s take a look at some commonly used methods to operate the registry

 The prototype of the method to create a subkey is:


 public RegistryKey CreateSubKey(string sunbkey);



 The parameter sunbkey represents the subkey to be created name or pathname. If the creation is successful, the created subkey is returned, otherwise null is returned.

 The prototype of the method to open a subkey is:


 public RegistryKey OpenSubKey(string name);
 public RegistryKey OpenSubKey(string name, bool writable);


 The parameter name represents the name of the subkey to be opened or its path name, The parameter writable indicates whether the opened subkey is allowed to be modified. The subkey opened by the first method is read-only. The Microsoft.Win32 class also provides us with another method for opening the registry on the remote computer. The method prototype is:


  public static RegistryKey OpenRemoteBaseKey(RegistryHive hKey, string machineName);


  Method for deleting subkeys The prototype is:


 public void DeleteKey(string subkey);


 This method is used to delete the specified primary key. If the subkey to be deleted also contains the primary key, the deletion will fail and an exception will be returned. If you want to completely delete the subkey and the subkeys in the directory, you can use the method DeleteSubKeyTree. The method prototype is as follows:


 public void DeleteKeyTree(string subkey ; An object type that returns null if the specified key does not exist. If it fails and you do not want the returned value to be null, you can specify the parameter defaultValue. If a parameter is specified, the value specified by the parameter will be returned if the reading fails.

 The prototype of the method for setting key values ​​is as follows:


 public object SetValue(string name,object value);

The above is the content of the C# registry operation implementation code. For more related content, please pay attention to the PHP Chinese website (www.php .cn)!




Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Active Directory with C# Active Directory with C# Sep 03, 2024 pm 03:33 PM

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Access Modifiers in C# Access Modifiers in C# Sep 03, 2024 pm 03:24 PM

Guide to the Access Modifiers in C#. We have discussed the Introduction Types of Access Modifiers in C# along with examples and outputs.

Random Number Generator in C# Random Number Generator in C# Sep 03, 2024 pm 03:34 PM

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

C# Data Grid View C# Data Grid View Sep 03, 2024 pm 03:32 PM

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

C# StringReader C# StringReader Sep 03, 2024 pm 03:23 PM

Guide to C# StringReader. Here we discuss a brief overview on C# StringReader and its working along with different Examples and Code.

Patterns in C# Patterns in C# Sep 03, 2024 pm 03:33 PM

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

C# StringWriter C# StringWriter Sep 03, 2024 pm 03:23 PM

Guide to C# StringWriter. Here we discuss a brief overview on C# StringWriter Class and its working along with different Examples and Codes.

BinaryWriter in C# BinaryWriter in C# Sep 03, 2024 pm 03:22 PM

Guide to BinaryWriter in C#. Here we discuss syntax and explanation, how it works with examples to implement with proper codes.

See all articles