Click here to Skip to main content
11,924,280 members (56,144 online)
Click here to Skip to main content
Add your own
alternative version


21 bookmarked

C# Wallpaper Switcher

, 28 Nov 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
Windows service code to change wallpaper at configured intervals


This is a simple Windows service which switches the wallpapers at configured intervals.


The intention of this article is to make my/your free time a little bit useful.

Using the Code

The start() method of this Windows service does the configuration and starts the timer. The ConfigReader class is for reading the XML file which is included in the attached zip file.

public void Start()
//Get the config file path
configPath = Assembly.GetExecutingAssembly().Location;
int i = configPath.LastIndexOf("\\");
configPath = configPath.Substring(0, i) + "\\AppConfig.xml";

// Create, configure & Start timer 
t = new Timer(1000 * double.Parse(WallpaperChanger.ConfigReader.GetConfigValue
    (configPath, "TimerIntervalSeconds")));
t.Elapsed += new ElapsedEventHandler(t_Elapsed);

//Get the image files in an array
arrFiles = System.IO.Directory.GetFiles(WallpaperChanger.ConfigReader.GetConfigValue
    (configPath, "WallpaperDirectory"), "*.bmp", 

Assembly.GetExecutingAssembly().Location gives you the path of the current assembly. AppConfig.xml configuration file has the interval and the directory of the wallpapers. This AppConfig.xml should be in the same directory of the EXE file.

t = new Timer(1000 * double.Parse
    (WallpaperChanger.ConfigReader.GetConfigValue(configPath, "TimerIntervalSeconds")));

This line initializes the timer with the configured interval from AppConfig.xml.

t.Elapsed += new ElapsedEventHandler(t_Elapsed);

Create a handler for Elapsed event will be fired at every interval:

arrFiles = System.IO.Directory.GetFiles(WallpaperChanger.ConfigReader.GetConfigValue
    (configPath, "WallpaperDirectory"), "*.bmp", System.IO.SearchOption.AllDirectories);

This line gets the full path of all the BMP image files in the directory which is configured in the AppConfig.xml.

t.Start(); starts the timer.

The t_Elapsed() method will be called based on the configured regular interval. This function does the main job as stated in this article's title.

void t_Elapsed(object sender, ElapsedEventArgs e)
//Set the wallpaper based on global variable currentIndex
if (currentIndex < arrFiles.Length)
WinAPI.SystemParametersInfo(WinAPI.SPI_SETDESKWALLPAPER, 1, 
    arrFiles[currentIndex], WinAPI.SPIF_SENDCHANGE);
currentIndex = 0;

The WINAPI class is shown below:

public class WinAPI
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SystemParametersInfo
    (int uAction, int uParam, string lpvParam, int fuWinIni);
public const int SPI_SETDESKWALLPAPER = 20;
public const int SPIF_SENDCHANGE = 0x2;

The following system call sets the desktop with the image file given in arrFiles[currentIndex].

WinAPI.SystemParametersInfo(WinAPI.SPI_SETDESKWALLPAPER, 1, 
    arrFiles[currentIndex], WinAPI.SPIF_SENDCHANGE);

Points of Interest

This is meant to make you smile. If it does not, this article is not for you.


  • 28th November, 2007: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
India India
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionAny body know how to set wallpaper inside window service Pin
dilhaniranaweera5-Jan-13 22:04
memberdilhaniranaweera5-Jan-13 22:04 
AnswerRe: Any body know how to set wallpaper inside window service Pin
Member 1041105019-Nov-13 23:21
memberMember 1041105019-Nov-13 23:21 
BugNot change the Wallpaper like Windows Service Pin
laynusfloyd22-Jul-12 6:51
memberlaynusfloyd22-Jul-12 6:51 
Generalhi Pin
sandeepparekh2-Jun-11 22:19
membersandeepparekh2-Jun-11 22:19 
GeneralProblem with Vista Pin
maxald11-Mar-09 3:19
membermaxald11-Mar-09 3:19 
GeneralCPU Usage Pin
Sarafian29-Nov-07 2:48
memberSarafian29-Nov-07 2:48 
For a time now i thought of writing my own application about changing a wallpaper.
The basic reason is that i haven't found one that takes into consideration whether the cpu is occupied like when playing a game. In this case the switch results in a glitch in the game which is very annoying.

So I would suggest that you implement this feuture. Check the CPU for a custom o predermined usage and then change the wallpaper.

GeneralRe: CPU Usage Pin
shanAtCP29-Nov-07 5:33
membershanAtCP29-Nov-07 5:33 
GeneralSuggestion Pin
maharishi_b28-Nov-07 21:11
membermaharishi_b28-Nov-07 21:11 
GeneralRe: Suggestion [modified] Pin
shanAtCP28-Nov-07 21:20
membershanAtCP28-Nov-07 21:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151125.3 | Last Updated 29 Nov 2007
Article Copyright 2007 by shanAtCP
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid