Wallpaper Changer Command Line Utility

.NET 60 Comments »

Description

WallpaperChanger is a command line utility for changing the wallpaper (desktop background) in Windows. The intent of this program is not to be a standalone wallpaper program (it is too cumbersome to use for that). It is intended to be used as a “helper” utility program for another program or script.

For example, I wrote this program for use with a Java program I was writing to change the desktop wallpaper. I ran into a problem trying to get Java to change the desktop wallpaper. The best way to do it is to use one of the Windows system libraries (dlls). I decided to implement a command line utility program in C# to set a new wallpaper. My Java program executed this external utility program when it set the wallpaper.

This utility works fine with Windows XP, Vista, Windows 7, Windows 8, and Windows 10.

Use

The program takes two arguments: the file (including path) to use as wallpaper, and the style (Tiled, Centered, Stretched, Fit, Fill). Instead of a file, a directory can also be specified in which case a random image from the directory will be set as the wallpaper.

Syntax is: [file|directory] [style] [location]

  [file] is the complete path to the file
  [directory] is the complete path to a directory containing image files
    a random image from the directory will be set as the background
  [style] is an integer (if no style is specified it defaults to Stretched):
    0 for Tiled
    1 for Centered
    2 for Stretched
    3 for Fit (Windows 7 or later)
    4 for Fill (Windows 7 or later)
  [location] is the complete path to a directory for storing the generated file
    defaults to the temp folder which should be fine in most cases

If the style argument is not specified it will default to Stretched.

Optional flags:

  • -h, -help – Display the usage help
  • -r, -remove – Remove the current wallpaper
  • -m, -monitor <index> – Set the image on the specified monitor (0 indexed)

When using the monitor option, the full syntax is:

-m <index> <file|directory> <location>

The style does not need to be specified since it appears to always default to Stretched.  Note this functionality is only available on Windows 8 or higher.

Alternatively a config file can be placed in the same directory as the WallpaperChanger executable. The file should be named ‘config’ without
any file extension. Each line in the file should have the full path to an image and can optionally include the monitor index or the style code to use. If the style is not specified it will default to Stretched.

When setting the monitor index in the config file the format of the line should be: <file> -m <index>

The file type can be any of the standard picture types (png, bmp, jpg, gif, etc.). The program will automatically convert the file to a png/bmp file and place it within the users temp directory. Storing the png/bmp file in the temp directory should be fine in most cases, however if you would prefer to use an alternative directory you can specify it as parameter number 3.  On Windows 8 or higher it will use a png file for better quality.  It seems Windows 7 and lower cannot use a png file directly so a bmp file is used instead.  Note: On Windows 7 (and lower) if you are using extremely high resolution images then you may run into the “artifact” problem after Windows sets the file as the background.  If you notice artifacts then my recommendation is to use a separate program to lower the resolution of the original image or try saving it as a different file type first (e.g. jpg).

When using the config file it should be formatted like this:

C:\wallpaper1.jpg
C:\wallpaper\wallpaper2.jpg 3
D:\wallpaper3.png 2
"D:\wallpapers to use\image1.jpg"
C:\wallpaper4.jpg -m 1
etc.

Full paths to the image files should be used and if the path or filename contains spaces you will need to wrap it in quotes.

The program has a return code – 0 for normal, 1 for error

An error code will be returned if the program had an exception, for example invalid syntax, invalid file name, invalid style type, etc.

Requirements

Because WallpaperChanger is written in C#, it requires the .NET Framework (specifically version 2.0 or later) to be installed. If you do not have the .NET Framework installed, you can get it from Windows Update or from the Microsoft website.

Download

Download the WallpaperChanger here (current version: 1.8)

Source Code

I’ve made the source code available on GitHub at https://github.com/philhansen/WallpaperChanger

I currently build the program using Microsoft Visual Studio 2017 Community edition. It targets the .NET Framework v2.0 as that is all it requires.

What’s New

  • 1.8 – Config file now supports the monitor index flag to set the wallpaper for a specific monitor (Windows 8 or higher)
  • 1.7 – Added ability to set the wallpaper for a specific monitor (Windows 8 or higher)
  • 1.6 – Use png file for Windows 8 or higher, otherwise use bmp file
  • 1.5 – Wallpaper is now converted to png instead of bmp to avoid artifacts
  • 1.4 – Added remove flag and config file support
  • 1.3 – Added two new styles for Windows 7 or later (Fit and Fill)

Disclaimer

This program is free for use and distribution. It is delivered “as-is”. I have tested the program and have used it without problem in my own wallpaper changing program.

The code in the program was based off of the code in a CodeProject article found at:
http://www.codeproject.com/dotnet/SettingWallpaperDotNet.asp

FileSystemWatcher doesn’t work on Vista

.NET, Vista No Comments »

The FileSystemWatcher class is part of the .NET framework and is used to watch for changes in directories or files.  For programs that need to monitor particular files, this class is perfect and very helpful.  I wrote a program to parse a log file and used the FileSystemWatcher class to monitor the log file  and raise an event when a new line is added to the log.  My code worked great on Windows XP but after upgrading to Windows Vista I noticed that the FileSystemWatcher stopped working.

The FileSystemWatcher seems to have a bug on Vista where  if the file is currently open by another program (as in the case with real-time log files) then it will not notice any changes made to the file.  From my own experience I found that if I used Windows Explorer to browse to the log folder (or refreshed the folder if Windows Explorer was already there) then the FileSystemWatcher would finally pick up the changes.  I experienced this bug on .NET 2.0 so I’m not certain if it exists in .NET 1.1 or 3.0 on Vista.

I eventually came up with a workaround for my particular program.  You basically need to add  your own timer which will check the file for modifications.  You could create your own code to act as a FileSystemWatcher and look for new lines added.  But this involves rewriting your code.  Another solution is to use a FileInfo object to help the FileSystemWatcher.  In my program I created a timer that would fire once per second.  The timer creates a FileInfo object for the same log file that the FileSystemWatcher is currently watching.  It will then call the Refresh method of the FileInfo object.  Calling the Refresh method will cause the FileSystemWatcher to notice any changes to the file and raise the proper event like it is supposed to.  Since the timer code is so short there is no noticeable performance increase.  This is the preferred workaround since you don’t need to modify any of your existing code.  Additionally, whenever this bug is fixed you can just remove the timer and you will not have to rewrite any code.  If your program also runs on other operating systems (like XP or 2003 server) then you could probably add some additional code to check which operating system is running and only use the timer code if it is running on Vista.

I discussed this particular bug with others on the MSDN forums at: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1569013&SiteID=1

© Phillip Hansen | Original theme by N.Design Studio
Entries RSS Comments RSS Log in