Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Registry value throws UnauthorizedAccessException [duplicate]

Tags:

I have a C# application and I am trying to edit a service through the registry. I am using a manifest file that requires administrator privileges to run my application. Despite that, this code throws

System.UnauthorizedAccessException: Cannot write to the registry key.

RegistryKey key = Registry.LocalMachine.OpenSubKey ("SYSTEM\\CurrentControlSet\\services\\Tomcat7");
key.SetValue ("Start", 2, RegistryValueKind.DWord);

Does anybody have any ideas for how to fix this?

like image 795
Nik Avatar asked Apr 26 '12 19:04

Nik


2 Answers

Follow the following code, note the additional true argument:

RegistryKey key = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\services\\Tomcat7",true);
key.SetValue("Start", 2, RegistryValueKind.DWord);
like image 114
SHOIKAT CHOWDHURY Avatar answered Oct 14 '22 06:10

SHOIKAT CHOWDHURY


This might help ,

Link to similar issue on stack overflow

looks like you are opening the key read only.

like image 28
Eric Brown - Cal Avatar answered Oct 14 '22 07:10

Eric Brown - Cal