SaveSetting Procedure

   
SaveSetting Procedure

Class

Microsoft.VisualBasic.Interaction

Syntax

 SaveSetting(   appname   ,   section   ,   key   ,   setting   ) 
appname (required; String)

The name of the application

section (required; String)

The name of the registry key

key (required; String)

The name of the value entry whose value is to be saved

setting (required; String or numeric)

The value to save

Description

Creates or saves an entry for a VB application in the Windows registry

Rules at a Glance

  • If either the appname or section subkeys are not found in the registry, they are automatically created.

  • The function writes a value to a subkey of the KEY_CURRENT_USER\Software\ VB and VBA Program Settings key of the registry.

  • section need not be an immediate subkey of appname ; instead, section can be a fully qualified path to a nested subkey, with each subkey separated from its parent by a backslash. For example, a value of Settings\Coordinates for the section argument indicates that the value is to be retrieved from HKEY_CURRENT_USER\Software\VB and VBA Program Settings\appname\Settings\Coordinates .

  • Visual Basic writes setting to the registry as a string ( REG_SZ ) value. If setting is not a string, VB attempts to coerce it into a string in order to write it.

  • If the setting cannot be saved, a runtime error will be generated.

Programming Tips and Gotchas

  • The built-in registry-manipulation functions allow you to create professional 32-bit applications that use the registry for holding application-specific data, in the same way that .INI files were used in the 16-bit environment. You can, for example, store information about the user 's desktop settings (i.e., the size and position of forms) the last time the program was run.

  • Since it writes to the current user's registry key, SaveSetting should be used exclusively for storing user settings; it should not be used to store nonuser information (i.e., hardware information, system-level information, or application information that is independent of the user).

  • GetSetting , GetAllSettings , and SaveSetting allow you direct access to only a limited section of the Windows registry, that being a special branch created for your application ( HKEY_CURRENT_USER\Software\VB and VBA Program Settings\ yourappname ) .

  • SaveSetting does not allow you to write to the default value of a registry key. Attempting to do so produces runtime error 5, "Invalid procedure call or argument." This is not as great a limitation as it may appear, since GetSetting also cannot retrieve a default value from a registry key.

  • This may seem obvious, but it has been often overlooked: if a user hasn't run the application before and your application's initialization doesn't set up the registry structure for the application, the key values won't be there.

  • The previous point is particularly applicable when running your application on Windows in a multiuser environment since Microsoft chose to use the HKEY_CURRENT_USER branch of the registry to store entries for VB applications. This means that your application can be running swimmingly for one user, but when another user logs onto the machine, the registry settings are not available.

  • Rather than rely on the relatively underpowered registry-access functionality available in Visual Basic, we highly recommend that you instead use the Registry and RegistryKey classes available in the BCL's Microsoft.Win32 namespace.

See Also

DeleteSetting Procedure, GetAllSettings Function, GetSetting Function

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net