Use Visual Basic. NET to access the Registry

Source: Internet
Author: User

Introduction

When programming in Visual Basic. NET, you can access the Registry through functions provided by Visual Basic. NET or the Registry class of the. NET Framework. Although Visual Basic functions are sufficient in most cases, the. NET Framework is still required.

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 check the code that accesses the Registry to ensure that the computer that runs the code will not have a security impact.

The Registry Key consists of two parts: Value Name and value. Projects are stored in the item and sub-item systems, just as files in directories and sub-directories are stored in the file system.

Prerequisites

To master the content of this article, you must meet the following prerequisites:

  • Familiar with earlier Visual Basic versions.
  • Master the design and usage of the Registry.
  • Understand the security issues of accessing the registry.
Use the Visual Basic. NET function to access the Registry

Visual Basic. NET provides four functions to access the registry. To use these functions, you must haveRegistryPermissionAccessEnumeratedReadAndWritePermission. Any code running with full trust is based on the default security principle, which means that any code installed on the user's local hard drive has permission to access the registry.

Visual Basic. NET functions Description
DeleteSetting Delete one or more item settings from the project of the application in the registry.
GetSetting Return an item setting value from the project of the application in the registry.
GetAllSettings Return the list of item settings and their values from the project of the application in the registry.
SaveSetting Create or save an application project in the registry.

Note:Cannot pass GetSettingThe name of the return segment of the function.

If you cannot save the item settings,ArgumentExceptionObject.

The following example creates a registry key and two subkeys. The value of the first item is displayed, and the value of the first item and its subitem is displayed. Delete the second sub-item, and display the value of the first item and its sub-item to confirm that the second sub-item is deleted.

'Create the first entry.
SaveSetting ("TestApp", "Startup", "FirstKey", "First ")
'Create the first subitem.
SaveSetting ("TestApp", "FirstKey", "FirstSubKey", "FirstSub ")
'Create the second subitem.
SaveSetting ("TestApp", "FirstKey", "SecondSubKey", "SecondSub ")
Try
'Write the value of the first item.
Console. WriteLine (GetSetting ("TestApp", "Startup", "FirstKey "))
'Write the first item and its two subitems.
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
End Try

Using the built-in registry to access functions has the following restrictions:HKEY_CURRENT_USER \ Software \ VB and VBA Program Settings. Therefore, you must log on to the system becauseHKEY_CURRENT_USERThe registry key is activated only when you log on to the system.

Registry Settings accessed from a non-interactive process such as mtx.exe should be stored inHKEY_LOCAL_MACHINE \ Software \OrHKEY_USER \ DEFAULT \ SoftwareUnder the Registry.

Use the. NET Framework to access the Registry

BecauseGetSettingAndSaveSettingThe function can only accessHKEY_CURRENT_USER \ Software \ VB and VBA Program SettingsSo it has some limitations. Therefore, you can use the. NET Framework in the Microsoft. Win32 namespaceRegistryAndRegistryKeyClass.

RegistryClass provides basic registry items for accessing subkeys and their values. The base item itself is read-only. The following table lists and describesRegistryClass.

Item Description
ClassesRoot Define the document type and the features related to these types.
CurrentConfig Contains hardware configuration information unrelated to the user.
CurrentUser Contains information about the current user's preferences, such as environment variables.
DynData Contains dynamic registry data, such as the registry data used by the Virtual Device Driver.
LocalMachine Contains five sub-items: Hardware, SAM, Security, Software, and System.
PerformanceData Contains the performance information of software components.
Users Contains information about 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 Is Nothing) Then
IntVersion = regVersion. GetValue ("Version", 0)
RegVersion. Close ()
End If


The following example reads, increments, and writes a DWORD Value to 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
'This item does not exist. Create this item.
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


Permission

System. Security. PermissionIn the namespaceRegistryPermissionClass controls the ability to access registry variables. Registry variables should not be stored inRegistryPermissionThe memory location that the code can access. Similarly, when granting permissions, only the minimum permissions required to complete the work should be granted.

Registry access limitRegistryPermissionAccessEnumeration definition. The following table details the items.

Value Description
AllAccess Create, read, and write access to registry variables.
Create Access the creation of registry variables.
NoAccess You cannot access registry variables.
Read Access to the Registry variables.
Write Write access to registry variables.

Note:You can use OrTo achieve this, as shown below:
RegistryPermissionAccess. Write Or RegistryPermissionAccess. Read _ "HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ TestApp"

Use Registry Editor to access registry

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

Access Registry Editor

  1. Open a deployment project.
  2. InViewView) in the menu, pointEditorEditor), and then clickRegistryRegistry ).
Summary

The Registry is the best place to store information about applications and user settings. You can also view information about system hardware or applications to interact.

For most projects, accessing the Registry through the Visual Basic runtime function is sufficient. However, in some cases, you may also need to use the. NET FrameworkRegistryClass andRegistryKeyClass. These two operations are simple. However, because such access involves security issues, you must avoid security risks, such as including a plain text password or other sensitive information.


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.