Udostępnij za pośrednictwem


How to: Create a Registry Key and Set Its Values in Visual Basic

The CreateSubKey method of the My.Computer.Registry object can be used to create a registry key.

Procedure

To create a registry key

  • Use the CreateSubKey method, specifying which hive to place the key under as well as the name of the key. The parameter Subkey is not case-sensitive. This example creates the registry key MyTestKey under HKEY_CURRENT_USER.

    My.Computer.Registry.CurrentUser.CreateSubKey("MyTestKey")
    

To create a registry key and set a value in it

  1. Use the CreateSubkey method, specifying which hive to place the key under as well as the name of the key. This example creates the registry key MyTestKey under HKEY_CURRENT_USER.

    My.Computer.Registry.CurrentUser.CreateSubKey("MyTestKey")
    
  2. Set the value with the SetValue method. This example sets the string value. "MyTestKeyValue" to "This is a test value".

    My.Computer.Registry.SetValue("HKEY_CURRENT_USER\MyTestKey", _
    "MyTestKeyValue", "This is a test value.")
    

Example

This example creates the registry key MyTestKey under HKEY_CURRENT_USER and then sets the string value MyTestKeyValue to This is a test value.

My.Computer.Registry.CurrentUser.CreateSubKey("MyTestKey")
' Change MyTestKeyValue to This is a test value. 
My.Computer.Registry.SetValue("HKEY_CURRENT_USER\MyTestKey", _
"MyTestKeyValue", "This is a test value.")

Robust Programming

Examine the registry structure to find a suitable location for your key. For example, you may want to open the HKEY_CURRENT_USER\Software key of the current user, and create a key with your company's name. Then add the registry values to your company's key.

When reading the registry from a Web application, the current user depends on the authentication and impersonation implemented in the Web application.

It is more secure to write data to the user folder (CurrentUser) rather than to the local computer (LocalMachine).

When you create a registry value, you need to decide what to do if that value already exists. Another process, perhaps a malicious one, may have already created the value and have access to it. When you put data in the registry value, the data is available to the other process. To prevent this, use the GetValue method. It returns Nothing if the key does not already exist.

It is not secure to store secrets, such as passwords, in the registry as plain text, even if the registry key is protected by ACLs (Access Control Lists).

The following conditions may cause an exception:

Security

To run this process, your assembly requires a privilege level granted by the RegistryPermission class. If you are running in a partial-trust context, the process might throw an exception due to insufficient privileges. Similarly, the user must have the correct ACLs for creating or writing to settings. For example, a local application that has the code access security permission might not have operating system permission. For more information, see Code Access Security Basics.

See Also

Tasks

Troubleshooting: Manipulating the Registry

Concepts

Common Registry Tasks

Code Access Security Basics

Reference

My.Computer.Registry Object

My.Computer.Registry.CurrentUser Property

CreateSubKey

Other Resources

Walkthrough: Creating a Registry Key and Changing Its Values