program to randomly set the screensaver

wga22

Member
Joined
Apr 16, 2003
Messages
10
This program demonstrates how to get a file listing, then set a value to the registry. When run, maybe through a daily scheduled task, this program looks at all of the *.scr files in your SYSTEM directory and sets to the registry one of the file names as your current screensaver.
 

Attachments

No question, just sharing code! :) I am contemplating adding the ability to set random wallpapers. It would require a simple gui to let the user choose images to use randomly. It could store the paths in an xml file, or maybe create a registry key.
 
Here is the code, so you can see how simple it is; without downloading:

Code:
namespace Bobbyber
{
	using System;
	using System.IO;
	using Microsoft.Win32;

 
	public class ConsoleApp
	{
		public static int Main(string[] args)
		{
			//writeURLs
			//HKCU\\SOFTWARE\\MICROSOFT\\INTERNET EXPLORER\\TYPEDURLS
			RegistryKey keyCurrentMachine = Registry.CurrentUser ;
			RegistryKey keySoftware = keyCurrentMachine.OpenSubKey ("Control Panel\\Desktop", true);
			try
			{
				keySoftware.SetValue("Scrnsave.exe", getScreenSaverName(Environment.SystemDirectory));
				Console.WriteLine ("Screensaver is now :" + keySoftware.GetValue("Scrnsave.exe"));
				keySoftware.SetValue("ScreenSaveTimeOut", "300");
				Console.WriteLine("Screensaver timeone now 5 minutes");
				//keySoftware.SetValue("Scrnsave.exe", getScreenSaverName("c:\\winnt\\system32\\"));
			}
			catch (Exception e)
			{
				Console.WriteLine(e.Message);
			}
			return 0;
		}
		    
    	public static string getScreenSaverName(string targetDirectory) 
    	{
			// Process the list of files found in the directory
			string [] fileEntries;
			try
			{
				fileEntries = Directory.GetFiles(targetDirectory, "*.scr");
			}
			catch(Exception e)
			{
				Console.WriteLine(e.Message);
				Console.WriteLine("not happening for dir:" + targetDirectory);
				return "";
			}
			Random asdf = new Random();
			return (string)fileEntries[asdf.Next(0,fileEntries.Length)];
	    }
		
		public static void writeURLs()
		{
			RegistryKey theCurrentMachine = Registry .CurrentUser ;
			RegistryKey theSoftware = theCurrentMachine.OpenSubKey ("SOFTWARE");
			RegistryKey theMicrosoft = theSoftware.OpenSubKey ("Microsoft");
			RegistryKey theIE = theMicrosoft.OpenSubKey ("Internet Explorer");
			RegistryKey theTypedURLS = theIE.OpenSubKey ("TypedURLs");
			//Now get all the values in the key...
			long lCount = theTypedURLS.ValueCount;
			if(lCount <= 0) return;
			string [] arTypedURL = theTypedURLS.GetValueNames();
			Console.WriteLine ("You have typed in following web sites in IE :");
			int i = 1;
			foreach ( string theURL in arTypedURL)
			{
				//get the name of the url ...
				Console.WriteLine ("[{0}] {1}",i,theTypedURLS.GetValue (theURL));
				i++;
			}		
		}
	}
}
 
Thank you so much for sharing that. Does the screen saver actually change in windows so that when you open up display the new screen saver is set?
 
Yes. The screensaver changes inside of the display properties menu. The program runs in the console (I have mine in my startup). BTW, it would be easy to randomly set the wallpaper as well as it is also a key in the registry.
 
Back
Top