Back to doug's directory

File information
Filename: SetDesktopBackground.cs Uploaded: Sat, 29th May 2010 19:58:00
Size (bytes): 1.8 KiB md5 checksum: f87f33aee8c8f4e0b3b210b6430f3ee8
Uploader doug Download: SetDesktopBackground.cs
Description:

Windows command line tool that sets the desktop background wallpaper. It takes a single argument that is the path to the picture file.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Runtime.InteropServices;
 
/**
 * A command line tool to set the desktop background wallpaper.
 * Takes a single argument that is the filename to the wallpaper to set.
 * Author: doug@neverfear.org
 * Date: 2010-05-29
 */
namespace SetDesktopBackground
{
    static class Program
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
 
        private static readonly int MAX_PATH = 260;
        private static readonly int SPI_GETDESKWALLPAPER = 0x73;
        private static readonly int SPI_SETDESKWALLPAPER = 0x14;
        private static readonly int SPIF_UPDATEINIFILE = 0x01;
        private static readonly int SPIF_SENDWININICHANGE = 0x02;
 
        static string GetDesktopWallpaper()
        {
            string wallpaper = new string('\0', MAX_PATH);
            SystemParametersInfo(SPI_GETDESKWALLPAPER, (int)wallpaper.Length, wallpaper, 0);
            return wallpaper.Substring(0, wallpaper.IndexOf('\0'));
        }
 
        static void SetDesktopWallpaper(string filename)
        {
            SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, filename, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
        }
 
        static void Main(string[] args)
        {
            System.Console.WriteLine("Current desktop wallpaper is at path: " + GetDesktopWallpaper());
            try
            {
                SetDesktopWallpaper(args[0]);
            }
            catch (IndexOutOfRangeException ex)
            {
                System.Console.WriteLine("Not enough parameters supplied. Please supply a filename.");
            }
        }
    }
}
 
Powered by Debian, Guinness, and excessive quantities of caffeine and sugar.