CreateSubKey method of the
My.Computer.Registry object can be used to create a registry key.
To create a registry key
CreateSubKeymethod, specifying which hive to place the key under as well as the name of the key. The parameter
Subkeyis not case-sensitive. This example creates the registry key
To create a registry key and set a value in it
CreateSubkeymethod, specifying which hive to place the key under as well as the name of the key. This example creates the registry key
Set the value with the
SetValuemethod. 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.")
This example creates the registry key
MyTestKey under HKEY_CURRENT_USER and then sets the string value
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.")
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.
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
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.