Access the registry using Visual Basic. NET

Source: Internet
Author: User
Tags file system integer variables tostring win32
visual| Access | Registration Form

When you are programming in Visual Basic. NET, you can choose to access the registry through a function provided by Visual Basic. NET or a registry class of the. NET Framework. Although it is sufficient to use the Visual Basic function in most cases, it is sometimes necessary to use the. NET framework.

The registry stores information about the operating system and the applications installed on the computer. Using the registry may affect security. Therefore, you must carefully examine the code that accesses the registry to ensure that it does not have a security impact on the computer that will run the code.

A registry key consists of two parts: a value name and a value. Projects are stored in the items and child systems as if they were stored in directories and subdirectories in the file system.

Prerequisite conditions

To master this article, you must have the following prerequisites: Familiarize yourself with previous versions of Visual Basic. Knowledge of the design and application of the registry. Understand security issues accessing the registry.

Visual Basic. NET provides four functions to access the registry. To use these functions, you must have RegistryPermissionAccess permissions. Any code that runs as full trust (based on the default security policy, which means any code installed on the user's local hard drive) has access to the registry.

Visual Basic. NET functions Description
DeleteSetting Deletes a paragraph or an item setting from the application's project in the registry.
GetSetting Returns an item setting value from the application's project in the registry.
GetAllSettings Returns a list of item settings and their values from the application's project in the registry.
SaveSetting Create or save an application project in the registry.


Note: You cannot return the name of a segment through the GetSetting function.

If you cannot save the item settings, a ArgumentException object is generated.

The following example creates a registry key and two subkeys. It then displays the value of the first item and displays the value of the first item and its subkeys. It then deletes the second subkey and displays the value of the first and its subkeys to confirm that the second subkey was deleted.

' Create the first item.

SaveSetting ("TestApp", "Startup", "Firstkey", "a")

' Create the first subkey.

SaveSetting ("TestApp", "Firstkey", "Firstsubkey", "Firstsub")

' Create a second subkey.

SaveSetting ("TestApp", "Firstkey", "Secondsubkey", "Secondsub")

Try

' Write the value of the first item.

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

' Writes out the first item and 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 ("TestApp", "Startup", "Firstkey"))

Console.WriteLine (GetAllSettings ("TestApp", "Startup")

Catch e As ArgumentException

Catch e as Exception

Console.WriteLine (e.gettype.tostring)

Finally

The registry key under the end try. To do this, you must be logged on to the system because the HKEY_CURRENT_USER registry key is activated only when you log on to the system.

Registry settings that are accessed from a non-interactive process, such as Mtx.exe, should be stored under the Hkey_local_machine\software\ registry key.

Using the. NET Framework to access entries under the Registry has certain limitations. To do this, you can use the. NET Framework to Microsoft.Win32 registry classes in namespaces.

The Registry class provides a basic registry key for accessing subkeys and their values. The base item itself is read-only. The following table lists and describes the seven items that are available in the registry class.

Item Description
Classesroot Defines the type of document and the attributes associated with those types.
Currentconfig Contains hardware configuration information that is not user-independent.
CurrentUser Contains information about preferences for the current user, such as environment variables.
DynData Contains dynamic registry data, such as registry data used by virtual device drivers.
LocalMachine Contains five subkeys (hardware, SAM, security, Software, and System) to store configuration data for the local computer.
PerformanceData Contains performance information for software components.
Users Contains information about the default user preferences.

The following example shows how to read a DWORD 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 are nothing) Then

Intversion = Regversion.getvalue ("Version", 0)

Regversion.close ()

End If



The following example reads, increments, and then writes a DWORD value to the 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

' does not exist, create the item.

Regversion =

Registry.CurrentUser.CreateSubKey ("software\\microsoft\\testapp\\1.0")

End If

Dim intversion as Integer = 0

If (not regversion are nothing) Then

Intversion = Regversion.getvalue ("Version", 0)

Intversion = intversion + 1

Regversion.setvalue ("Version", intversion)

Regversion.close ()

End If



Permissions

The RegistryPermission class in the System.Security.Permission namespace controls the ability to access registry variables. Registry variables should not be stored in memory locations that do not have registrypermission code to access. Similarly, when granting permissions, only the minimum permissions required to complete the work should be granted.

Registry access values are defined by the RegistryPermissionAccess enumeration. The following table describes each of these in detail.

value Description
AllAccess Create, read, and write access to registry variables.
Create Create access to registry variables.
NoAccess Cannot access registry variables.
Read Read access to registry variables.
Write Write access to the registry variable.


Note: If you need some kind of permission combination, such as permission to read and write access, and deny creation of access, you can do so by using an OR operation, as follows:

Registrypermissionaccess.write Or Registrypermissionaccess.read _ "Hkey_local_machine\software\microsoft\testapp"


Using Registry Editor to access the registry

When you use a deployment project, you can also choose to use Registry Editor to specify registry keys and key values to be added to the registry of the target computer.

Access Registry Editor to open a deployment project. In the View menu, point to Editor (editor), and then click Registry (Registry).

The registry is the best place to store information about applications and individual user settings. In addition, you can view information about your system hardware or the application you want to interact with.

For most projects, it is sufficient to access the registry through the Visual Basic run-time functions. In some cases, however, you may also want to use the registry class of the. NET Framework. Both operations are simple, but because this access involves security issues, be careful not to create a security risk, such as including a plain text password or other sensitive information in the item.

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.