Access | registry
Access the registry using vb.net
Translator Note: There are more examples of access to the registry, however, there are not many examples of access to the registry through vb.net, this article translated an MSDN use of vb.net access to the registry example, very detailed also quite comprehensive. (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchimpdragdrop.asp)
Cat Francis
Visual Studio Team
Microsoft Corporation
April 2002
Summary: This article describes the use of vb.net built-in functions deletesetting, GetAllSettings, GetSetting and SaveSetting, and the common language runtime's two classes registry and RegistryKey to access the instance of the registry, detailing the required permissions, explaining when to use Registry Editor, and showing you how to use the program to dynamically read data from the registry and write data.
Introduction
When you are programming with vb.net, you may choose to access the registry using functions from the registry class in the VB.net or. NET Framework. While in most cases vb.net's built-in functions are sufficient, in some cases you still need the. NET Framework classes to solve the problem.
The registry stores not only information about some programs on the local machine, but also information about the operating system. Operating the registry can be dangerous. Therefore, you must carefully review the code while you are programming to ensure that the program does not pose a threat to the security of the machine you are running.
Registry entry points include two parts: key names and key values. Entry points are keys and subkeys stored in the system, similar to directories and subdirectories in the file system.
Essential knowledge
To read this article need to have the following knowledge:
1, familiar with the previous version of Visual Basic.
2. The knowledge of the design and utilization of the registry.
3, understand the security meaning of access to the registry.
Accessing the registry with vb.net built-in functions
Vb. NET provides four functions to access the registry, in order to use them, you must first have read and write permissions. Any code that is 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 function
Describe
DeleteSetting
Delete an item or key value in the application in the registry
GetSetting
Returns the key value of the application entry point in the registry
GetAllSettings
Returns a column of key values for a program entry point in the registry
SaveSetting
Create or save a program's settings in the registry
Note: You cannot return the name of an item through the GetSetting function
If the settings for an item cannot be saved, a ArgumentException object is triggered. See ArgumentException for more relevant information.
The following example creates a primary key and two subkeys in the registry. First print the value of the primary key, and then output the value of the primary key and its two subkeys, and then delete the second subkey, in the output primary key and the value of the first subkey to determine whether to delete the second subkey.
' Create the '.
SaveSetting ("TestApp", "Startup", "Firstkey", "a")
' Create the ' subkey.
SaveSetting ("TestApp", "Firstkey", "Firstsubkey", "Firstsub")
' Create the second subkey.
SaveSetting ("TestApp", "Firstkey", "Secondsubkey", "Secondsub")
Try
' Write the ' the ' s value.
Console.WriteLine (GetSetting ("TestApp", "Startup", "Firstkey"))
' Write ' the ' the ' the ' the ' as ' 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 ("TestApp", "Startup", "Firstkey"))
Console.WriteLine (GetAllSettings ("TestApp", "Startup")
Catch e As ArgumentException
Catch e as Exception
Console.WriteLine (e.gettype.tostring)
Finally
End Try
There is a limit to the registry access function built in vb.net you can access only the registry keys under HKEY_CURRENT_USER\Software\VB and VBA program settings, and you must log in to the system to access entries under that entry. Because the HKEY_CURRENT_USER primary key is inaccessible without being logged in.
Accessing the registry with the. NET Framework
As mentioned above, the registry access function built in vb.net can only access the registry keys under the HKEY_CURRENT_USER\Software\VB and VBA program settings, so the application is limited. At this time, you can use. NET Framework to access the registry in the Microsoft.Win32 namespace of the registry and RegistryKey classes. See also Registry and RegistryKey.
Security Notice: Writing data to the current user's registry (Microsoft.Win32.Registry.CurrentUser) is more secure than writing data to the local machine's registry (Microsoft.Win32.Registry.LocalMachine). In 21 cases, this is typical squatting when the key value you are creating is already being created by another potentially malicious process. For this to happen, the RegistryKey.GetValue method is used to get the key value, and the method returns nothing when a key does not exist.
The registry class provides some ways to access the registry primary key and its subkeys, which are read-only. The following table lists the methods that the registry class provides to access the registry
Key
Description
Classesroot
Defines the types that are associated with documents and properties
Currentconfig
Contains hardware configuration information that is not part of a specific user
CurrentUser
Contains information about the current user parameter, such as environment variables
DynData
Contains dynamic registry data, such as data used by some virtual device drives
LocalMachine
The configuration data for the local machine is stored, including five subkeys hardware, SAM, security, Software, and System
PerformanceData
Includes performance information for 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 are nothing) Then
Intversion = Regversion.getvalue ("Version", 0)
Regversion.close ()
End If
The next example is read from the HKEY_CURRENT_USER, add, and write a DWORD value:
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 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 manipulates the ability to access registry variables. Registry variables are stored in areas of memory that only RegistryPermission code can access. Similarly, when permissions are granted, they are given only the smallest permission to complete the task. See RegistryPermission and System.Security.Permissions.
Registry access values are defined by the RegistryPermissionAccess enumeration, see registrypermissionaccess For more information, and the following table lists only its members.
Value
Description
AllAccess
The right to create, read, and write registry variables.
Create
The right to create a registry variable.
NoAccess
The registry cannot be accessed.
Read
Permission to read the registry variable.
Write
The right to write to the registry variable.
Note: If you need a combination of permissions, for example, if you need to read and write without creating a new permission, you can use the following bitwise operator, as in the following example:
Registrypermissionaccess.write Or Registrypermissionaccess.read _
"Hkey_local_machine\software\microsoft\testapp"
Using Registry Editor to access the registry
When you configure your project, you may want to use Registry Editor to add key values to the registry of the target computer. Refer to Registry Editor for more information.
To access Registry Editor:
1, open a configuration project.
2, find the View menu, point to Editor, and then click Registry.
Use the registry in configuration engineering to refer to registry Settings Management in deployment.
Conclusion:
The registry is an ideal place to store application information and user personal settings, and you can check the registry for information about the system hardware or programs that your program is interacting with.
In most cases, it's enough to access the registry through the VB.net run-time function, but sometimes you might use the registry and RegistryKey classes of the. NET Framework. Both types of access to the registry are simple, but there are security issues with this approach that you must be cautious about, such as you cannot include plaintext passwords or other sensitive information in a key.
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.