c# cannot change registry value

G

Guest

I have Visual Studio 2005 Express Edition. I am trying to change registry
value by:

RegistryKey key = Registry.CurrentUser.OpenSubKey("Software\\My_app");
key.SetValue(name_of_value, new_value_string);

I am getting error:


System.UnauthorizedAccessException: Cannot write to the registry key.
at System.ThrowHelper.ThrowUnauthorizedAccessException(ExceptionResource
resource)
at Microsoft.Win32.RegistryKey.SetValue(String name, Object value,
RegistryValueKind valueKind)
at Microsoft.Win32.RegistryKey.SetValue(String name, Object value)
at MyApp.Form1.MyFunction(String data, String name_of_value) in
C:\Documents and Settings\Admin\MyApp\MyApp\Form1.cs:line 70



what is wrong?
 
E

Eric Renken

To change the value you should be using CreateSubey instead of OpenSubKey

Eric Renken
 
W

Willy Denoyette [MVP]

Chris said:
I have Visual Studio 2005 Express Edition. I am trying to change registry
value by:

RegistryKey key = Registry.CurrentUser.OpenSubKey("Software\\My_app");
key.SetValue(name_of_value, new_value_string);

I am getting error:


System.UnauthorizedAccessException: Cannot write to the registry key.
at System.ThrowHelper.ThrowUnauthorizedAccessException(ExceptionResource
resource)
at Microsoft.Win32.RegistryKey.SetValue(String name, Object value,
RegistryValueKind valueKind)
at Microsoft.Win32.RegistryKey.SetValue(String name, Object value)
at MyApp.Form1.MyFunction(String data, String name_of_value) in
C:\Documents and Settings\Admin\MyApp\MyApp\Form1.cs:line 70



what is wrong?


.......OpenSubKey("Software\\My_app", true);

Willy.
 
B

Bud Bundy

When you use OpenSubKey you are opening registry keys in read-only mode. Just make this change to your code,

RegistryKey key = Registry.CurrentUser.OpenSubKey("Software\\My_app", true);

Good luck!!

EggHeadCafe.com - .NET Developer Portal of Choice
http://www.eggheadcafe.com
 
U

umesh chape

I want to do following task by using c#.

Run - regedit
Navigate path - HKEY_CURRENT_USER\SOFTWARE\MICROSOFT\OFFICE\12.0\EXCEL\SECURITY
Rightclick --> new>DWORD>
add key ExtensionHardening
value = 0

For that propose i write following code:

RegistryKey regkey;
regkey = Registry.CurrentUser.OpenSubKey("HKEY_CURRENT_USER\\SOFTWARE\\MICROSOFT\\OFFICE\\12.0\\EXCEL\\SECURITY", true);

But i get regkey value is null.

what is wrong in above code.
 
S

Stanimir Stoyanov

"HKEY_CURRENT_USER" should not be included in your registry key path because
it is relative (in this case you are already using Registry.CurrentUser so
it is redundant). =>

RegistryKey regkey =
Registry.CurrentUser.OpenSubKey("SOFTWARE\\MICROSOFT\\OFFICE\\12.0\\EXCEL\\SECURITY",
true);
--
Stanimir Stoyanov
http://stoyanoff.info

in message
news:[email protected]...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top