Training
Module
Store and retrieve data using literal and variable values in C# - Training
Use data in your applications by creating literal values and variable values of different data types.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
The CreateSubKey
method of the My.Computer.Registry
object can be used 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")
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")
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.")
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.")
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).
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.
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Training
Module
Store and retrieve data using literal and variable values in C# - Training
Use data in your applications by creating literal values and variable values of different data types.