How to programmatically change the screen saver?

I want to change the current Screensaver for a user (which was previously loaded as a resource in Visual Studio) using C #. How can I do that? I searched it on Google and SO, but it all talks about how to create a “Screensaver,” not “How to Change a Screensaver.” If possible, it should work on WinXP, Vista, and 7.

+6
source share
2 answers

I will answer my question using the code that worked with me:

public sealed class Screensaver
{
    Screensaver() { }

    const int SPI_SETSCREENSAVEACTIVE = 0x0011;

    [DllImport("user32", CharSet=CharSet.Auto)]
    unsafe public static extern short SystemParametersInfo (int uiAction, int uiParam, int* pvParam, int fWinIni);

    public static void Set(string path)
    {
        try
        {
            RegistryKey oKey = Registry.CurrentUser.OpenSubKey("Control Panel",
            true);
            oKey = oKey.OpenSubKey("desktop", true);
            oKey.SetValue("SCRNSAVE.EXE", path);
            oKey.SetValue("ScreenSaveActive", "1");

            unsafe
            {
                int nX = 1;
                SystemParametersInfo(
                SPI_SETSCREENSAVEACTIVE,
                0,
                &nX,
                0
                );
            }
        }
        catch (Exception exc)
        {
            System.Windows.Forms.MessageBox.Show(exc.ToString());
        }
    }
}

Then, calling it from my application:

static string ResourcePath(string resource)
{
    return Application.StartupPath + "\\Resources\\" + resource;
}

Program.Screensaver.Set(Program.ResourcePath("svr1.scr"));

- , 8 ( , XP ), svr1.scr ( -, )

+5

,

rundll32.exe desk.cpl,InstallScreenSaver %l
0

All Articles