views:

453

answers:

4

I'd like to invoke the user's screen saver if such is defined, in a Windows environment. I know it can be done using pure C++ code (and then the wrapping in C# is pretty simple), as suggested here.
Still, for curiosity, I'd like to know if such task can be accomplished by purely managed code using the dot net framework (version 2.0 and above), without p/invoke and without visiting the C++ side (which, in turn, can use windows API pretty easily).

A: 

Controlling the Screen Saver with C#.

jinsungy
That's an awfully thin wrapper....
overslacked
Missed the point of the question.
sean e
+1  A: 

I think having a .Net library function that does this is highly unlikely - I'm not aware of any. A quick search returned this Code Project tutorial which contains an example of a managed wrapper which you mentioned in your question.

P/invoke exists so that you're able to access OS-specific features, of which screen savers are an example.

Charlie Salts
A: 

I'm not sure you can use completely managed code to do this.

This uses Windows API but is still very simple: http://stackoverflow.com/questions/267728/launch-system-screensaver-from-c-windows-form

Nate Bross
+3  A: 

I've an idea, I'm not sure how consistently this would work, so you'd need to research a bit I think, but hopefully it's enough to get you started.

A screen saver is just an executable, and the registry stores the location of this executable in HKCU\Control Panel\Desktop\SCRNSAVE.EXE

On my copy of Vista, this worked for me:

RegistryKey screenSaverKey = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop");
if (screenSaverKey != null)
{
    string screenSaverFilePath = screenSaverKey.GetValue("SCRNSAVE.EXE", string.Empty).ToString();
    if (!string.IsNullOrEmpty(screenSaverFilePath) && File.Exists(screenSaverFilePath))
    {
        Process screenSaverProcess = Process.Start(new ProcessStartInfo(screenSaverFilePath, "/s"));  // "/s" for full-screen mode
        screenSaverProcess.WaitForExit();  // Wait for the screensaver to be dismissed by the user
    }
}
Matthew Brindley