|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer|
How to: Create a Key In the Registry (Visual C#)
This example adds the value pair, "Name" and "Isabella", to the current user's registry, under the key "Names".
Examine the registry structure to find a suitable location for your key. For example, you might want to open the 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.
The following conditions might cause an exception:
The name of the key is null.
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.
It is more secure to write data to the user folder — Microsoft.Win32.Registry.CurrentUser — rather than to the local computer — Microsoft.Win32.Registry.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.Overload:Microsoft.Win32.RegistryKey.GetValue method. It returns null 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 access control lists (ACL).