I want to set value for a registry key. When I try to use SetValue method it throws UnauthorizedAccessException exception, that says:
Attempted to perform an unauthorized operation.
This is my code:
var key = Microsoft.Win32.Registry.CurrentUser
.OpenSubKey("SOFTWARE")
.OpenSubKey("Microsoft")
.OpenSubKey("Windows")
.OpenSubKey("CurrentVersion")
.OpenSubKey("Run", true);
if (key.GetValue("MyKey") == null)
{
key.SetValue("MyKey", localPath, Microsoft.Win32.RegistryValueKind.String);
}