Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't write to HKEY_CURRENT_USER registrykey in C#

Tags:

c#

.net

registry

I've searched google and the answers here for a while now and come to the conclusion that when writing to the registry, HKEY_CURRENT_USER should be available without UAC, correct? This is what every thread seem to state anyway. My problems is that I get an access denied error when trying to access this key and writing a new value. Is this due to there being something wrong with my registry or am I doing something wrong? Here's the code I'm using:

string runKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
RegistryKey startupKey = Registry.CurrentUser.OpenSubKey(runKey, true);
startupKey.SetValue("Arebokerts application", '"' + Environment.CurrentDirectory +     "\\arebokert.exe" + '"', Microsoft.Win32.RegistryValueKind.String);

Could anybody be so kind and shed some light on the subject? I want to stay away from any kind of UAC prompt and be able run it without UAC privileges. This code throws an "UnauthorizedAccessException". What is wrong?

Best regards, arebokert

EDIT

As David and Hans in the comments pointed out, this problem was due to something blocking me from changing the key. I found out it was comodo antivirus, and so I turned it off. This made the application function properly. Thank you for all responses!

like image 890
arebokert Avatar asked Oct 01 '13 14:10

arebokert


1 Answers

Your code is fine and standard user, by default, can write to HKCU. The problem is that the system is denying you write access to that part of the registry. The most likely explanations are:

  1. The machine on which you run this code has had that particular key secured to limit write access. Check if this is the case from regedit. Navigate to the key, right click on the key in the tree view and select Permissions. Select your user name and see what permissions you have. You should have Allow checked for both Full Control and Read.

  2. You have an anti-virus or anti-malware product running that is blocking write access to that key. Since this key is widely used by malware it's not uncommon for anti-malware products to fight back. You can diagnose whether or not this is the cause by disabling all anti-virus or anti-malware products temporarily.

like image 67
David Heffernan Avatar answered Nov 11 '22 18:11

David Heffernan