Access the registry with VB.NET

zhaozj2021-02-16  49

Access the registry with VB.NET

Translator Note: There are not many examples of accessing the registry, however, there are not many examples accessing the registry through VB.NET. This article translated an example of using VB.NET access registry on an MSDN. of. (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchimpdragdrop.asp)

Cat Francisvisual Studio Teammicrosoft Corporation

April 2002

Summary: This article describes the instance of the registry to access the registry, the universal language run, and the universal language runtime, and the universal language runtime, and the general language runtime. When you use the Registry Editor and show you how to use the program from the registry read data and how to write data.

introduction

When programming with VB.Net, you may choose to access the registry in the Registry class in the VB.NET or .NET framework. Although most cases the built-in function of VB.NET is enough, in some cases you still need the .NET framework class to solve the problem.

The registry not only stores information about some programs on the local machine, but also saves the information of the operating system. Operational registry may be dangerous. So you must be cautious when programming, ensuring that the programs do not constitute a threat to the security of the running machine.

The registry entry point includes two parts: key name and key value. The entry point is the key and sub-keys stored in the system, similar to the directory and subdirectories in the file system.

Prerequisite

If you want to read this article, you need to have the following must-have knowledge:

1. Familiar with the previous version of Visual Basic.

2, the knowledge of the design and utilization of registry.

3. Understand the security meaning of accessing the registry.

Use VB.NET Built-in Functions Access Registry

VB.NET provides a function of four access registry, in order to use them, you must first have read and write permissions. Any code running in full trust mode must have the necessary permissions to access the registry. You can view the RegistryPermission class to get more information.

VB.NET functions

description

Deletesetting

Delete an item or key value in the app in the registry

Getsetting

Returns the key value of the application entry point in the registry

GetallSettings

Returns a key value of the program entry point in the registry

Savesetting

Create or save the program in the registry

Note: You cannot return a name by getSetting function.

If you cannot save a setting, an ArgumentException object will be triggered. See argumentException to get more information.

The following example creates a primary key and two sub-keys in the registry. First print the value of the main key, then output the value of the primary key and its two sub-keys, then delete the second subkey, the value of the primary key and the first subkey, determine whether the second subkey is deleted.

'CREATE The first key.

Savesetting ("TestApp", "Startup", "Firstkey", "first")

'CREATE The first Subkey.

Savesetting ("TestApp", "FirstKey", "FirstsubKey", "Firstsub")

'Create The Second Subkey.

Savesetting ("TestApp", "Firstkey", "SECONDSUBKEY", "SECONDSUB")

Try

'WRITE The First Key's Value.

Console.writeline ((GetSetting ("TestApp", "Startup", "Firstkey"))

'WRITE The First Key As Well As ITS Two Subkeys.

Console.writeline (GetallSettings ("TestApp", "Startup"))

Catch e as argumentexception

Catch e as exception

Console.writeline (E.GETTYPE.TOSTRING)

Finally

END TRY

DELETESETTING ("TestApp", "Firstkey", "SecondsubKey")

Try

Console.writeline (GetSetting, "Startup", "Firstkey"))

Console.writeline (GetallSettings ("TestApp", "Startup"))

Catch e as argumentexception

Catch e as exception

Console.writeline (E.GETTYPE.TOSTRING)

Finally

END TRY

The registry access function with VB.NET built-in is restricted. You can only access the registry key under HKEY_CURRENT_USER / SOFTWARE / VB AND VBA Program Settings, and to access the entry under this item must be logged in to the system because The HKEY_CURRENT_USER key key is not accessible in unregos.

Access the registry with .NET framework

As mentioned above, the registry access function built into VB.NET can only access the registry key under HKEY_CURRENT_USER / SOFTWARE / VB AND VBA Program Settings, so the application is limited. At this time, you can access the registry with the Registry and RegistryKey classes in the .NET framework Microsoft.Win32 namespace. See REGISTRY and REGISTRYKEY.

Safety Notice: Write data to the current user's registry (Microsoft.Win32.REGISTRY.CURRENTUSER) to write data more secure than to the registry of the local machine (Microsoft.Win32.Registry.LocalMachine). There are two cases, that is, when you are created by the key value you are creating, this is a typical squat. For this happens, use the RegistryKey.getValue method to get the key value, the method will return noth when a key does not exist.

The Registry class provides some methods that can access the registry primary key and its sub-keys, the base bore itself is read-only. The following table lists the method of accessing the registry provided by the Registry class

Key

Description

Classessroot

Define types associated with documentation and properties

CurrentConfig

Contains hardware configuration information that does not belong to a specific user

Currentuser

Information containing current user parameters, such as environment variables

Dyndata

Contains dynamic registry data, such as data used in some virtual device drivers

LocalMachine

Store the configuration data of the local machine, including five sub-keys Hardware, SAM, Security, Software, and System

PerformanceData

Contains performance information of software components

Users

Contains information about the default user parameters

The following example shows how to read a DWORD key value from HKEY_CURRENT_USER: Imports Microsoft.Win32

Dim Regversion As RegistryKey

Dim KeyValue As String

KeyValue = Software // Microsoft // TestApp // 1.0

Regversion = registry.currentuser.opensubKey (KeyValue, False)

DIM intversion as integer = 0

IF (Not Regversion Is Nothing) THEN

INTVERSION = Regversion.getValue ("Version", 0)

Regversion.close ()

END IF

The next example is to read, add, and write a DWORD value from HKEY_CURRENT_USER:

Imports Microsoft.Win32

Dim Regversion As RegistryKey

Regversion =

Registry.currentuser.opensubkey ("Software // Microsoft // TestApp // 1.0", TRUE)

IF regversion is nothing then

'Key doesn't exist; create it.

Regversion =

Registry.currentuser.createSubkey ("Software // Microsoft // TestApp // 1.0")

END IF

DIM intversion as integer = 0

IF (Not Regversion Is Nothing) THEN

INTVERSION = Regversion.getValue ("Version", 0)

INTVERSION = intversion 1

Regversion.SetValue ("Version", intversion)

Regversion.close ()

END IF

Authority

The RegistryPermission class in the System.Security.Permission namespace manipulates the ability to access the registry variable. The Registry variable is stored in the memory area that only the registrypermission code can access. Similarly, when granting permissions, only the minimum permissions of the task are completed. See RegistryPermission and System.Security.Permissions.

Registry Access Ratings are defined by the RegistryPermissionAccess enumeration, see RegistryPermissionAccess get more information, the following table lists only its members.

Value

Description

Allaccess

Create, read, and write access to registry variables.

Create

Create permission for registry variables.

NoAccess

You cannot access the registry.

Reta

Read the permissions of registry variables.

Write

Write access to registry variables.

Note: If you need some permissions, for example, you need to read and write without requiring new permissions, you can use the following bit operand, such as the following example:

RegistryPermissionAccess.write or registrypermitmissionAccess.read_

"HKEY_LOCAL_MACHINE / SOFTWARE / Microsoft / TestApp"

Use the Registry Editor to access the registry

When you configure your project, you may have to use the registry editor to add key values ​​to the registry of the target computer. More information can be referred to the Registry Editor. Access Registry Editor:

1. Open a configuration project.

2, find the View menu, point to Editor, and then click Registry.

Use the registry in the configuration engineering to refer to Registry Settings Management in Deployment.

in conclusion:

The registry is an ideal location that stores application information and user personal settings, which can check the information of system hardware or programs that are interacting with your program through registry.

In most cases, the function accesses the registry through VB.NET is enough, but sometimes you may use the Registry and RegistryKey classes of the .NET framework. These two ways to access the registry are very simple, but this method has security issues, you must be cautious, for example, you can't include a plain text password or other sensitive information in a key.

转载请注明原文地址:https://www.9cbs.com/read-24217.html

New Post(0)