Detailed description of functions required for programming to solve the registry problem (1)

Source: Internet
Author: User
Tags function examples

Among various programming languages, Visual Basic is the easiest to use. This section focuses on VB programming. For VC, Delphi, and VFP, only a few examples are provided later.
Use Visual Basic to operate the Registry
There are two ways to process the Registry in VB: one is to use VB's own functions, and the other is to call Windows API functions to operate the registry.
1. operation functions on the registry in MicroSoft Visual Basic
Note: Within Visual Basic 6.0, a standard registration location (1) has been provided to store the program information of the application created in VB ): HKEY_CURRENT_USER \ Software \ VB and VBA Program settings ").


In MicroSoft Visual Basic, there are 4 registry operation functions, including SaveSetting, GetSetting, GetAllSettings, and DeleteSettings.
1. SaveSetting saves or creates an application project in the Windows Registry
Syntax SaveSetting appname, section, key, setting
Appname: A string expression that contains the name of an application or project. It is a sub-key in the standard position.
Section: A string expression that contains the region name. It is a subkey under appname.
Key: string expression, Value Name of the subkey of the standard position \ appname \ section ).
Setting: string expression that contains the key setting value.
Function example the following example uses the SaveSetting statement to create a project of the "My project" application in the Windows Standard location or the 16-bit Windows platform ini file, then, use the DeleteSetting statement to delete it. Because the key parameter is not specified, the entire sub-key will be deleted, including the sub-key name and all its key values ).
'Add some setting values in the standard position.
SaveSetting appname: = "My projects", section: = "My subkeys", key: = "Top", setting: = 75
SaveSetting "My projects", "my sub-keys", "Left", 50
'Delete subkeys and all set values.
DeleteSetting "My projects", "my sub-keys" ① add some settings in the registry. Use the SaveSetting statement to create a subkey named "My projects \ My subkeys" in the standard position.
SaveSetting appname: = "My projects", section: = "My subkeys", key: = "Top", setting: = 75
SaveSetting "My projects", "my sub-keys", "Left", 50
② Delete the sub-key and all set values.
DeleteSetting "My projects", "My subkeys"
2. GetSetting returns the registry key setting value from the application project in the Windows registry.
Syntax GetSetting appname, section, key [, default]
Appname: A string expression that contains the name of an application or project.
Section: A string expression that contains the region name. registry key settings are required for this region.
Key: string expression. The name set in the registry is returned.
Default: Optional. Expression. If no value is set in registry key settings, the default value is returned. If omitted, the default value is a string ("") with zero length ("").
If GetSetting does not have a parameter, GetSetting returns the default value.
The function example uses the SaveSetting statement to create a project for the appname application, and then uses the GetSetting function to obtain and display one of the settings. The GetSetting function must return a value because the default parameter is input. Note that the section name cannot be obtained using the GetSetting function. Finally, delete the application item using the DeleteSetting statement.
① Define the variable for saving the two-dimensional array data returned by the GetSetting Function
Dim MySettings As Variant
② Add a project to the Registry
SaveSetting "My projects", "my sub-keys", "Top", 75
SaveSetting "My projects", "my sub-keys", "Left", 50
Debug. Print GetSetting (appname: = "My projects", section: = "My subkeys", key: = "Left", default: = "25 ")
③ Delete a project in the Registry
DeleteSetting "My projects", "My subkeys"

3. GetAllSettings: return all registry item settings and their corresponding values of the application project from the Windows registry.
Syntax GetAllSettings (appname, section)
The appname parameter is required. String expression, the name of the application or project.
Section is required. String expression that contains the region name and requires registry key settings for the region. GetAllSettings returns Variant, which is a two-dimensional array of strings. The two-dimensional array contains all registry key settings and their corresponding values in the specified area.
If the appname or section does not exist, GetAllSettings returns the uninitialized Varian
Function example in this example, the SaveSetting statement is used to create the project of the appname application in the Windows Standard location, and then the GetAllSettings function is used to obtain and display the setting value. Note that the Application name and section name cannot be obtained using the GetAllSettings function. Finally, delete the application item using the DeleteSetting statement.
'Variable used to save the two-dimensional array data returned by the GetAllSettings Function
'Integer is used for counting.
Dim MySettings As Variant, intSettings As Integer
'Add the set value in the registry.
SaveSetting appname: = "My projects", section: = "My subkeys", key: = "Top", setting: = 75
SaveSetting "My projects", "my sub-keys", "Left", 50
'Get the set value of the input item.

MySettings = GetAllSettings (appname: = "My projects ",
Section: = "My subkeys ")
For intSettings = LBound (MySettings, 1)
UBound (MySettings, 1)
Debug. Print MySettings (intSettings, 0), MySettings (intSettings, 1)
Next intSettings
DeleteSetting "My projects", "My subkeys"

4. DeleteSetting deletes the region or registry key settings from the application project
Syntax: DeleteSetting appname, section [, key]
The appname parameter is required. String expression, the name of the application or project.
Section is required. String expression that contains the name of the region where the registry key is to be deleted. If only appname and section are available, the specified region and all related registry key settings are deleted.
Key is optional. String expression that contains the registry key settings to be deleted.
If all parameters are provided, delete the specified registry key settings. If you try to use the DeleteSetting statement set on a nonexistent region or registry key, a runtime error occurs.
For function examples, see SaveSetting function examples.


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.