Zayko in the Net

Personal blog of Vitaly Zayko


C#: How To Disable Windows Screensaver programmatically?

In some cases you may want to prevent Windows screensaver from running. For example, when a customer watch video or does VoIP call, his mouse and keyboard activities are low.

I didn’t find a managed function in .NET Framework but fortunately we can make it through PInvoke.

1. Add PInvoke namespace:

using System.Runtime.InteropServices;

2. Import SystemParametersInfo function and its enums as below:

public enum SPI : uint
public enum SPIF : uint
   None = 0x00,
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SystemParametersInfo(SPI uiAction, uint uiParam, ref uint pvParam, SPIF fWinIni);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SystemParametersInfo(SPI uiAction, uint uiParam, uint pvParam, SPIF fWinIni);

3. You may want to check is screensaver enabled by Control panel setting or not:

uint vParam = 0;
if (SystemParametersInfo(SPI.SPI_GETSCREENSAVEACTIVE, 0, ref vParam, SPIF.None))
   if (vParam == 1)
       MessageBox.Show("Screensaver is enabled");
        MessageBox.Show("Screensaver is disabled");

4. And finally - disable it:

if (SystemParametersInfo(SPI.SPI_SETSCREENSAVEACTIVE, 0, 0, SPIF.None))
   MessageBox.Show("Screensaver has been disabled");

5. Don’t forget to re-enable if it was enabled initially:

SystemParametersInfo(SPI.SPI_SETSCREENSAVEACTIVE, 1, 0, SPIF.None);

New version of ASP.NET AJAX Control Toolkit available!

Three new controls have been added:

  1. HTMLEditor - The HTMLEditor control allows you to easily create and edit HTML content. You can edit in design mode, as a rich text editor, or in source view to edit the HTML markup directly.
  2. ComboBox - The ComboBox control provides a DropDownList of items, combined with TextBox. Different modes determine the interplay between the text entry and the list of items.
  3. ColorPicker - The ColorPicker Control Extender can be attached to any ASP.NET TextBox control. It provides client-side color-picking functionality with UI in a popup control.

Download from here.

Where “Send To” in Windows 7?

Long time ago I have found useful to add some custom shortcuts to “Send To” folder in Windows. Most useful for me is shortcut to Notepad: if you have this shortcut in “Send To”, when you need to open any text file (or HTML, or XML – you name it) you just need to select that file in Windows Explorer, right click on it and under “Send To” select the Notepad shortcut. Simply, isn’t it?

It is not always easy to locate this folder. Because Windows 7 is coming and I already switched to Win7 RC, this tip applies to Win7. Here is the path: C:\Users\<user name>\AppData\Roaming\Microsoft\Windows\SendTo

Note: folder “AppData” is system and hidden by default, thus if you hate to type the entire path manually, open C:\Users\<user name>\ location then type “AppData” in address bar and then you can use your mouse to navigate to “SendTo”.

SendTo folder in Windows 7