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.
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.
To create a registry key and set a value in it
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.
Set the value with the SetValue method. This example sets the string value. "MyTestKeyValue" to "This is a test value".
This example creates the registry key MyTestKey under HKEY_CURRENT_USER and then sets the string value MyTestKeyValue to This is a test value.
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 () rather than to the local computer ( ).
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 themethod. 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:
The name of the key is Nothing ().
The user does not have permissions to create registry keys ().
The key name exceeds the 255-character limit ().
The key is closed ().
The registry key is read-only ().
To run this process, your assembly requires a privilege level granted by theclass. 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 .