Export (0) Print
Expand All

Reading from and Writing to the Registry Using Visual Basic Run-Time Functions

Visual Studio .NET 2003

Visual Basic .NET offers four functions to access the registry. To use them, you must have Read and Write permission from the RegistryPermissionAccess Enumeration enumeration. Any code running with full trust (under the default security policy, this is any code installed on the user's local hard drive) has the necessary permissions to access the registry. For more information, see RegistryPermission Class.

Visual Basic .NET Function Description
DeleteSetting Deletes a section or key setting from an application's entry in the registry.
GetSetting Returns a key setting value from an application's entry in the registry.
GetAllSettings Returns a list of key settings and their values from an application's entry in the registry.
SaveSetting Creates or saves an application entry in the registry.
Note   Section names cannot be returned via the GetSetting function.

If the key setting cannot be saved, an ArgumentException object is thrown. For more information on this exception, see ArgumentException Class.

The following example creates a registry key and two subkeys. It prints the first key's value and then prints the values of the first key as well as its subkeys. It then deletes the second subkey and prints the values of the first key and its subkeys to confirm the deletion of the second subkey.

' Create the first key.
SaveSetting("TestApp","Startup","FirstKey","First")
' Create the first subkey.
SaveSetting("TestApp","FirstKey","FirstSubKey","FirstSub")
' Create the second subkey.
SaveSetting("TestApp","FirstKey","SecondSubKey","SecondSub")
Try
   ' Write the first key's value.
   Console.WriteLine ((GetSetting("TestApp","Startup","FirstKey"))
   ' Write the first key as well 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 limitation to using the built-in registry access functions: You can access only the registry keys under HKEY_CURRENT_USER\Software\VB and VBA Program Settings. To do so, you must be logged onto the system because the HKEY_CURRENT_USER registry key is not active until you log on to the system.

Registry settings that are to be accessed from a noninteractive process (such as mtx.exe) should be stored under either the HKEY_LOCAL_MACHINE\Software\ or the HKEY_USER\DEFAULT\Software registry keys.

See Also

Reading from and Writing to the Registry Using the Microsoft.Win32 Namespace | DeleteSetting Function | GetSetting Function | GetAllSettings Function | SaveSetting Function | Code: Determining Whether a Registry Key Exists (Visual Basic) | Code: Adding Data to the Registry (Visual Basic) | Code: Creating a Registry Key and Setting Its Values (Visual Basic) | Code: Deleting Data from the Registry (Visual Basic) | Code: Deleting a Registry Key (Visual Basic) | Code: Reading Data from the Registry (Visual Basic)

Show:
© 2015 Microsoft