7

I'm working on a tool that needs to get the current user's wallpaper path.

On Windows 7, I can get that by reading

HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Desktop\General\WallpaperSource.

On my Windows 8 installation, that key always has the value

C:\Users\Peter\AppData\Roaming\Microsoft\Windows Photo Viewer\Windows Photo Viewer Wallpaper.jpg

which is not even the wallpaper that's currently set.

Is there any other key I can rely on?

Rob
  • 45,296
  • 24
  • 122
  • 150
Peter W.
  • 2,323
  • 4
  • 22
  • 42

4 Answers4

11

You are FAR better off calling SystemParametersInfo with the SPI_SETDESKWALLPAPER option to set the desktop wallpaper. As far as I can tell, registry key you're using is undocumented and thus can change at any time without warning.

See this stack overflow question for an example of how to call the SystemParametersInfo with SPI_SETDESKWALLPAPER.

Community
  • 1
  • 1
Larry Osterman
  • 16,086
  • 32
  • 60
  • And just where did the OP mentioned C++ or any other language for that matter? – Zuul Jul 01 '12 at 15:37
  • 1
    There is one documented mechanism to set the desktop wallpaper. Changing registry keys is (as far as I know) 100% undocumented, and thus is subject to change without notice. – Larry Osterman Jul 01 '12 at 16:02
  • Not saying that a more programmatically method wouldn't be the appropriate one, but a simple search at [MSDN](http://msdn.microsoft.com/en-US/) shows the key for [Desktop Registry Settings](http://msdn.microsoft.com/en-us/library/aa452934.aspx) documented. Also this Microsoft Support [Article ID: 329677](http://support.microsoft.com/kb/329677). – Zuul Jul 01 '12 at 16:25
  • That documentation is for WinCE which does not necessarily apply to desktop Windows. And the KB article applies to Windows XP only. – Larry Osterman Jul 01 '12 at 16:49
  • I didn't want to set the wallpaper programmatically, I just needed its path. But thanks anyway. – Peter W. Jul 01 '12 at 21:08
  • 4
    If you want it's path, use SPI_GETDESKWALLPAPER. The registry key can be changed at any point in the future without notice. – Larry Osterman Jul 02 '12 at 05:42
  • @Zuul - that documentation is for Windows CE 5.0. I sometimes (well, pretty much all the time!) think that the target platform / language version text on MSDN needs to be made more prominent, it's far too easy to overlook when you land somewhere from a google search =/ – Rob Jul 02 '12 at 07:43
  • @Rob, the link was an example, it seems that Microsoft doesn't like to document the registry keys to much, nonetheless, at very least since winXP (*will confirm on win98*), that key exists and is used for that goal, I don't see why a programmer can't rely on it. But I'm not to defend Microsoft and their philosophy, the OP goal was to know where it is stored for Win8, not to programmatically access it, and that's the only reason I've pointed out on my first comment here! – Zuul Jul 02 '12 at 07:50
  • I've said it, and I'll say again, @Larry Osterman is correct when talking about a rock solid solution for a programmer. But again, was not the question placed by Peter W. I've marked the question as **Off Topic**, since it should be transferred to **superuser.com**. It is questioning for a system detail, rather then a programmatically way of accessing/work with it. I suggest you all to do the same instead of insisting on something that hasn't been requested by the OP. – Zuul Jul 02 '12 at 08:00
  • 1
    @Zuul - Microsoft don't like to document it because it's an, as Larry points out (incidentally he works on Windows at Microsoft , *implementation detail* and is therefore subject to change. **The fact that it hasn't doesn't mean it won't.** The correct way to access it is via the documented method which is the API. Also, the first line of the OPs question reads "I'm **working on a tool** that needs to get the current user's wallpaper path." and the question was/is tagged c#/.net, why on earth should this be migrated off to superuser? – Rob Jul 02 '12 at 08:01
  • @Rob, please refer to my last comment! I've mentioned that what Larry pointed out is absolutely correct, but does not answer question placed! This question is Off Topic **as it stands**, please act accordingly. – Zuul Jul 02 '12 at 08:07
  • @Zuul - I'm asking you **how is this question off topic** ? The OP has stated **I'm working on a tool**, tagged it c#/.net. That makes it quite clear they're writing code, in c#, and want that code to retrieve information about the system. The OP also states *I can get that by reading*, not *I'm loading regedit*. It cannot possibly be any more on topic here, or any more **off topic** for superuser.com. It'd get closed in a shot over there as it stands. =) – Rob Jul 02 '12 at 08:11
  • As per the revision history, and as the question currently stands, it seems that I have no choice but to withdraw from this *battle*! Just not deleting my answer since the OP commented here, hours ago, that he doesn't want to access that key programmatically! – Zuul Jul 02 '12 at 08:18
10

Based heavily on the code available at pinvoke.net, the correct way to retrieve the current users desktop wallpaper is to use the SystemParametersInfo function. A sample of doing this is as follows:

using System;
using System.Runtime.InteropServices;

namespace WallpaperPathRetrieval
{
    class Program
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern Int32 SystemParametersInfo(UInt32 action, 
            UInt32 uParam, string vParam, UInt32 winIni);
        private static readonly UInt32 SPI_GETDESKWALLPAPER = 0x73;
        private static uint MAX_PATH = 260;

        static void Main(string[] args)
        {
            string wallpaper = new string('\0', (int)MAX_PATH);
            SystemParametersInfo(SPI_GETDESKWALLPAPER, MAX_PATH, wallpaper, 0);

            wallpaper = wallpaper.Substring(0, wallpaper.IndexOf('\0'));
        }
    }
}
Rob
  • 45,296
  • 24
  • 122
  • 150
6

The key you are mentioning isn't the correct one. Sounds to me that you've placed an image as your desktop background from Internet Explorer, and that key was opened to register it.

The correct key to get the desktop background location is: Confirmed on: XP, CE, Vista, 7, 8

HKEY_CURRENT_USER\Control Panel\Desktop\Wallpaper

Details:

  • Main key: HKEY_CURRENT_USER
  • Sub key: Control Panel\Desktop
  • Value name: WallPaper
  • Value type: REG_SZ
  • Value data: full path for the image being used as the desktop background

Also, under HKEY_CURRENT_USER\Control Panel\Desktop\ you will find other wallpaper related options to apply different styles: Center, Tile, and Stretch.

HKEY_CURRENT_USER\Control Panel\Desktop\WallpaperStyle
HKEY_CURRENT_USER\Control Panel\Desktop\TileWallpaper

In order to apply the styles use the following guide:

  1. Center

    WallpaperStyle = 0
    TileWallpaper = 0
    
  2. Tile

    WallpaperStyle = 0
    TileWallpaper = 1
    
  3. Stretch

    WallpaperStyle = 2
    TileWallpaper = 0
    
Zuul
  • 16,217
  • 6
  • 61
  • 88
  • Thanks, that key holds the correct wallpaper path on Windows 8. It's weird that it doesn't on Windows 7, even though I'm not setting the wallpaper through any browser. – Peter W. Jul 01 '12 at 13:33
  • In addition to those three styles, there is also Fit and Fill, with Fit having WallpaperStyle = 6, and Fill having WallpaperStyle = 10, with TileWallpaper = 0 for both. At least, that's what I've experienced on Windows 7. – AkselK Jul 04 '12 at 13:09
  • 1
    This doesnt work anymore. It's now stored as Binary under TranscodedWallpaper –  Feb 24 '15 at 09:56
0

It's stored in a value named TranscodedImageCache (REG_BINARY). Here is a VBScript that reads/converts to plain text and outputs the value.

How to Determine the Current Wallpaper File Name and Path in Windows 8 - The Winhelponline Blog

Ramesh
  • 1