469,284 Members | 2,507 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,284 developers. It's quick & easy.

c# cannot change registry value

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.ThrowUnauthorizedAccessExceptio n(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?
Feb 21 '07 #1
4 30629
To change the value you should be using CreateSubey instead of OpenSubKey

Eric Renken

"Chris" <Ch***@discussions.microsoft.comwrote in message
news:E3**********************************@microsof t.com...
>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.ThrowUnauthorizedAccessExceptio n(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?

Feb 21 '07 #2

"Eric Renken" <Er********@newsgroup.nospamwrote in message
news:O5**************@TK2MSFTNGP03.phx.gbl...
To change the value you should be using CreateSubey instead of OpenSubKey
Or at least use the OpenSubKey overload that allows you to request write
access.

http://msdn2.microsoft.com/en-us/library/ms128501.aspx
>
Eric Renken

"Chris" <Ch***@discussions.microsoft.comwrote in message
news:E3**********************************@microsof t.com...
>>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.ThrowUnauthorizedAccessExcepti on(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?


Feb 21 '07 #3
"Chris" <Ch***@discussions.microsoft.comwrote in message
news:E3**********************************@microsof t.com...
>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.ThrowUnauthorizedAccessExceptio n(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.
Feb 22 '07 #4
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
Feb 27 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Yogi_Bear_79 | last post: by
3 posts views Thread by caldera | last post: by
1 post views Thread by scorpion53061 | last post: by
2 posts views Thread by nikou_70 | last post: by
reply views Thread by Patrick F | last post: by
1 post views Thread by Patrick F | last post: by
4 posts views Thread by CDMAPoster | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.