0

I wish to set a wallpaper for Windows 7 using a C# service. This is working fine when the service is run as a console application. But after installing the service and starting it, then it does not switch between wallpapers. Anybody have an idea how to set the wallpaper inside the window service?

Here is my code:

private String file = @"C://Users//Alvin//Pictures//onepiece.jpg";

/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>
protected override void OnStart(string[] args)
{
    SetWallpaper(file, 0);
}

private void SetWallpaper(string WallpaperLocation, int WallpaperStyle)
{
    try
    {
        // Sets the actual wallpaper
        SystemParametersInfo(20, 0, "@" + WallpaperLocation, 0x01 | 0x02);
        // Set the wallpaper style to streched (can be changed to tile, center, maintain aspect ratio, etc.
        RegistryKey rkWallPaper = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
        // Sets the wallpaper style

        switch (walpaperStyle)
        {
            case 0:
                rkWallPaper.SetValue(@"WallpaperStyle", "0");
                rkWallPaper.SetValue(@"TileWallpaper", "1");
                break;
            case 1:
                rkWallPaper.SetValue(@"WallpaperStyle", "0");
                rkWallPaper.SetValue(@"TileWallpaper", "0");
                break;
            case 2:
                rkWallPaper.SetValue(@"WallpaperStyle", "2");
                rkWallPaper.SetValue(@"TileWallpaper", "0");
                break;
            case 3: // (Windows 7 and later)
                rkWallPaper.SetValue(@"WallpaperStyle", "6");
                rkWallPaper.SetValue(@"TileWallpaper", "0");
                break;
            case 4: // (Windows 7 and later)
                rkWallPaper.SetValue(@"WallpaperStyle", "10");
                rkWallPaper.SetValue(@"TileWallpaper", "0");
                break;
        }

        rkWallPaper.Close();
        cetakService("sukses set walpaper");
    }
    catch (Exception e)
    {
        cetakService("Error "+e.Message.ToString());
    }
}
Matthew Strawbridge
  • 19,940
  • 10
  • 72
  • 93
AlvinMeko
  • 1
  • 1

1 Answers1

1

If you wish to switch the wallpapers from time to time, please note that OnStart() is run once, when the service starts. I dont see how you're changing the wallpaper through SetWallpaper. It actually runs once and sets the wallpaper. No logic is implemented to keep changing the wallpaper for different cases to be executed. This code should change the wallpaper the first time the service starts, if that's what you wish to do, please make sure that your service has sufficient rights to access registry values.

Wasiq Ali
  • 251
  • 2
  • 11
  • Thank's for your answer : i put setWallpaper in metod onStart just to ensure that the code is working but at the time i start the service it does not change the wallpaper i actually using timer to change wallpaper every minutes, – AlvinMeko Nov 20 '13 at 11:38
  • Alright. Here's your problem. You're running the service under local system and trying to access HKEY_CURRENTUSER from the registry. Try accessing HKEY_USERS instead. It will give you a list of SIDs of the users of your machine. Each has a different Control Panel and wallpaper settings. Try finding your current user's SID and change the wallpaper from there. – Wasiq Ali Nov 20 '13 at 12:00