473,385 Members | 1,279 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Disable Screen Saver

Using C# I can determine if the Screen Saver is enabled by ...

private RegistryKey rkScreenSaver = Registry.CurrentUser.OpenSubKey(
@"Control Panel\Desktop" );
if ( (string) rkScreenSaver.GetValue( "ScreenSaveActive" ) == "1" )
{
}

I would like to disable it with ...

private RegistryKey rkScreenSaver = Registry.CurrentUser.OpenSubKey(
@"Control Panel\Desktop" );
if ( (string) rkScreenSaver.GetValue( "ScreenSaveActive" ) == "1" )
{
rkScreenSaver.SetValue( "ScreenSaveActive", "0" );
rkScreenSaver.Close( );
}

This throws an exception on the SetValue.

What is the "magic" that I am missing that will allow me to turn
ScreenSaveActive on and off?

--
-- Thom Little -- www.tlaNET.net -- Thom Little Associates, Ltd.
--

Nov 16 '05 #1
4 8284
Thom,

I'm not aware of a .Net Framework method that will control the screen saver.
You will probably have to do it with the Windows API. I always do it using
the SystemParametersInfo function, in user32.dll.

Hope this helps,

Dale

"Thom Little" <th**@tlanet.net> wrote in message
news:Om**************@TK2MSFTNGP09.phx.gbl...
Using C# I can determine if the Screen Saver is enabled by ...

private RegistryKey rkScreenSaver = Registry.CurrentUser.OpenSubKey(
@"Control Panel\Desktop" );
if ( (string) rkScreenSaver.GetValue( "ScreenSaveActive" ) == "1" )
{
}

I would like to disable it with ...

private RegistryKey rkScreenSaver = Registry.CurrentUser.OpenSubKey(
@"Control Panel\Desktop" );
if ( (string) rkScreenSaver.GetValue( "ScreenSaveActive" ) == "1" )
{
rkScreenSaver.SetValue( "ScreenSaveActive", "0" );
rkScreenSaver.Close( );
}

This throws an exception on the SetValue.

What is the "magic" that I am missing that will allow me to turn
ScreenSaveActive on and off?

--
-- Thom Little -- www.tlaNET.net -- Thom Little Associates, Ltd.
--

Nov 16 '05 #2
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.

Nov 16 '05 #3
Thank you for the help. I was lost in the maze (again).

I was constantly overlooking the second parameter in OpenSubKey. When I
included it and set it to true I was allowed to write the updated value.

It is now completed.

--
-- Thom Little -- www.tlaNET.net -- Thom Little Associates, Ltd.
--

""Jeffrey Tan[MSFT]"" <v-*****@online.microsoft.com> wrote in message
news:25**************@cpmsftngxa06.phx.gbl...
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.

Nov 16 '05 #4
Thom,

Please note that this should be concidered a quick "hack". If the
location
and/or name of the key is changed in newer versions of windows, then your
application will not run. The SystemParameterInfo API is more likely to stay
the same, even though I doubt the registry key will go anywhere, but who
knows with longhorn =)

//Andreas

"Thom Little" <th**@tlanet.net> skrev i meddelandet
news:%2****************@TK2MSFTNGP10.phx.gbl...
Thank you for the help. I was lost in the maze (again).

I was constantly overlooking the second parameter in OpenSubKey. When I
included it and set it to true I was allowed to write the updated value.

It is now completed.

--
-- Thom Little -- www.tlaNET.net -- Thom Little Associates, Ltd.
--

""Jeffrey Tan[MSFT]"" <v-*****@online.microsoft.com> wrote in message
news:25**************@cpmsftngxa06.phx.gbl...
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.


Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Thom Little | last post by:
Using C# I can determine if the Screen Saver is enabled by ... private RegistryKey rkScreenSaver = Registry.CurrentUser.OpenSubKey( @"Control Panel\Desktop" ); if ( (string)...
2
by: Dwight Trumbower | last post by:
When doing a SaveAS on workbook, within a c# program, and the screen saver has activated, I always get the error message: "Excel cannot complete this task with available resources". I've looked...
2
by: HumptyDumpty | last post by:
Does anyone know if there is a problem with re-enabling the Screen Saver after it has been disabled programmatically. I am using the SystemParametersInfo function within User32.dll, and have...
1
by: Daniel | last post by:
Hi all, i have a doubt related to the screen saver ability. After developing the asp.net webform, can i add screen saver ability in this webform? which means, i open the asp.net webform, after a...
2
by: Claire | last post by:
Hi, Our software runs on a XP system attached to a touch screen monitor and smartcard USB reader. We run our own simple corporate screen saver. When the screen is touched the screen saver...
4
by: =?Utf-8?B?SmltIE9ybXNiZWU=?= | last post by:
Hopefully my solution will help others. I have a MacBook Pro on which I am running WindowsXP Home Edition. It is running totally separate from the Mac OS. I have to reboot to switch between OS's....
1
by: ashwiniappajigowda | last post by:
Hi, I have an simple MFC dialog based application. On launch of that application 'Password protected screen saver' is not getting activated after the screen saver timeout. If 'On resume,...
3
by: KK | last post by:
Dear All I want to prevent the screen saver getting activated when my application is running. I have the following code, but still screen saver is activated. What corrections I should make to...
0
by: rehanrana | last post by:
I built application in VB6, my application appear in system tray, when I double click on icon of my application, it run screen saver, my screen saver shows images and news. Screen saver...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.