SoFunction
Updated on 2025-03-07

A small detail of the registry operation under c#

Let's look at a code with errors:

string subKeyName = @"Software\Microsoft\Windows\CurrentVersion\Run\"; //subkey name
string valueName = @"App Name"; //name of the more specific key that will hold the value, "" means (Default)
try
...{
RegistryKey reg = (subKeyName);
if (reg != null)
...{
(valueName);
();
}
}
catch (Exception ex)
...{
(this, ());
}
Execute this code and you can receive the following exception:

The reason is very simple:
(String) Retrieve child items in read-only mode
public RegistryKey OpenSubKey ( string name, bool writable)writable If write access permissions of items are required, set to true.
We need to bring the second parameter to indicate that we are open in a writable way.