I'd like to change the current Screensaver for a custom one (which I previously loaded as a resource in Visual Studio) using C#. How could that be done? I've looked for 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.
Asked
Active
Viewed 5,521 times
8
-
This one could be useful: http://bytes.com/topic/c-sharp/answers/263953-setting-up-screensaver-via-csharp-c-application You need to do it through registry – Andrey Marchuk Dec 05 '11 at 10:06
-
Thanks for your link. That eased the steps. However, it doesn't work for Windows XP (in Win7 it works like a charm). Do you know anything about that? – Sergi Juanola Dec 05 '11 at 17:37
2 Answers
6
I'll answer my question with the piece of code that worked to 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, when calling it from my application:
static string ResourcePath(string resource)
{
return Application.StartupPath + "\\Resources\\" + resource;
}
Program.Screensaver.Set(Program.ResourcePath("svr1.scr"));
I read somewhere I should write a name no longer than 8 characters (a bit weird, but XP is all like this), so my screensaver is called svr1.scr
(not really object oriented, but does the trick)

Sergi Juanola
- 6,531
- 8
- 56
- 93
-
1Ugly, but I can't find a better solution. `InstallScreenSaver` in `desk.cpl` displays the control panel UI afterwards, and `SystemParametersInfo` seems to not expose that feature. – CodesInChaos Dec 12 '11 at 11:50
1
This is the command that windows executes when installing a new one
rundll32.exe desk.cpl,InstallScreenSaver %l