Hi Thom,
Based on my understanding, you want to change the subkey value of Registry,
but you got an exception when changing it.
Actually, I think you may get UnauthorizedAccessException when using
RegistryKey.SetValue method. If you view RegistryKey.SetValue in MSDN, you
will see that:
"The key that is opened with the value being set must have been opened with
write access set, and not be a read-only key. Once you have been granted
write-access to a key, you can change the data associated with any of the
values in that key."
So your problem is that you open the registry key through readonly way, and
do not have the write access.
RegistryKey.OpenSubKey has 2 overloading type, for RegistryKey.OpenSubKey
Method (String) in MSDN, you will see:
"Retrieves a subkey as read-only."
So you should use another overloading type: RegistryKey.OpenSubKey Method
(String, Boolean), which the second parameter expresses the write access.
Do like this:
Microsoft.Win32.RegistryKey rkScreenSaver =
Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@" Control Panel\Desktop",
true );
if ( (string)rkScreenSaver.GetValue( "ScreenSaveActive" ) == "1" )
{
rkScreenSaver.SetValue( "ScreenSaveActive", "0" );
rkScreenSaver.Close( );
}
It should work for you.
===================================
Please apply my suggestion above and let me know if it helps resolve your
problem.
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.