PC Review


Reply
Thread Tools Rate Thread

c# cannot change registry value

 
 
=?Utf-8?B?Q2hyaXM=?=
Guest
Posts: n/a
 
      21st Feb 2007
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?
 
Reply With Quote
 
 
 
 
Eric Renken
Guest
Posts: n/a
 
      21st Feb 2007
To change the value you should be using CreateSubey instead of OpenSubKey

Eric Renken

"Chris" <(E-Mail Removed)> wrote in message
news:E3BEB827-3494-4E90-AB9D-(E-Mail Removed)...
>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?



 
Reply With Quote
 
Ben Voigt
Guest
Posts: n/a
 
      21st Feb 2007

"Eric Renken" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:E3BEB827-3494-4E90-AB9D-(E-Mail Removed)...
>>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?

>
>



 
Reply With Quote
 
Willy Denoyette [MVP]
Guest
Posts: n/a
 
      22nd Feb 2007
"Chris" <(E-Mail Removed)> wrote in message
news:E3BEB827-3494-4E90-AB9D-(E-Mail Removed)...
>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.


 
Reply With Quote
 
Bud Bundy
Guest
Posts: n/a
 
      27th Feb 2007
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
 
Reply With Quote
 
umesh chape
Guest
Posts: n/a
 
      19th Dec 2008
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.


 
Reply With Quote
 
Stanimir Stoyanov
Guest
Posts: n/a
 
      19th Dec 2008
"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

"umesh chape" wrote in message
news:(E-Mail Removed)...
>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.
>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Registry Tip of The Day - How to Change The Windows 2008 Terminal Services Licensing Mode Using The Registry Jon Wallace Microsoft Windows 2000 Registry 0 4th Oct 2008 07:06 PM
Registry Change Not Seen DC Microsoft Windows 2000 Registry Archive 4 23rd Nov 2004 10:03 PM
registry change dpwclk Windows XP Security 1 6th Jul 2004 07:12 AM
Registry change Rajesh_rana@hotmail.com Microsoft Windows 2000 2 18th Mar 2004 01:25 PM
unwanted registry change to change home page Mike L Windows XP Internet Explorer 1 15th Aug 2003 01:36 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:26 AM.