How to: Create a Registry Key and Set Its Value 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 parameterSubkey
is not case-sensitive. This example creates the registry keyMyTestKey
under HKEY_CURRENT_USER.My.Computer.Registry.CurrentUser.CreateSubKey("MyTestKey")
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 keyMyTestKey
under HKEY_CURRENT_USER.My.Computer.Registry.CurrentUser.CreateSubKey("MyTestKey")
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:
The name of the key is
Nothing
(ArgumentNullException).The user does not have permissions to create registry keys (SecurityException).
The key name exceeds the 255-character limit (ArgumentException).
The key is closed (IOException).
The registry key is read-only (UnauthorizedAccessException).
.NET Framework 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.
Note
Code Access Security (CAS) has been deprecated across all versions of .NET Framework and .NET. Recent versions of .NET do not honor CAS annotations and produce errors if CAS-related APIs are used. Developers should seek alternative means of accomplishing security tasks.