using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; using Microsoft.Win32; namespace RobvanderWoude { internal class GetCurrentWallpapers { static readonly string progver = "1.01"; static bool openwallpapers = false; static bool singlewallpaper = false; static bool verbosedisplay = false; static int Main( string[] args ) { #region Parse Command Line foreach ( string arg in args ) { switch ( arg.ToUpper( ) ) { case "/?": return ShowHelp( ); case "/O": if ( openwallpapers ) { return ShowHelp( "Duplicate command line switch /O" ); } openwallpapers = true; break; case "/S": if ( singlewallpaper ) { return ShowHelp( "Duplicate command line switch /S" ); } singlewallpaper = true; break; case "/V": if ( verbosedisplay ) { return ShowHelp( "Duplicate command line switch /V" ); } verbosedisplay = true; break; default: return ShowHelp( "Invalid command line argument \"{0}\"", arg ); } } #endregion Parse Command Line Dictionary wallpapers = new Dictionary( ); #region List Wallpapers try { using ( RegistryKey key = Registry.CurrentUser.OpenSubKey( @"Control Panel\\Desktop" ) ) { byte[] imagebytes = (byte[])key.GetValue( "TranscodedImageCache" ); string image = Bytes2Path( imagebytes ); if ( !wallpapers.Keys.Contains( image ) ) { wallpapers.Add( image, @"HKEY_CURRENT_USER\Control Panel\Desktop\TranscodedImageCache" ); } if ( !singlewallpaper ) { // Check if more cached values are available foreach ( string valuename in key.GetValueNames( ) ) { if ( valuename.StartsWith( "TranscodedImageCache_" ) ) { imagebytes = (byte[])key.GetValue( valuename ); image = Bytes2Path( imagebytes ); if ( image != null && !wallpapers.Keys.Contains( image ) ) { wallpapers.Add( image, @"HKEY_CURRENT_USER\Control Panel\Desktop\" + valuename ); } } } } } } catch { } // ignore errors #endregion List Wallpapers #region Return Results int maxlen = 0; if ( verbosedisplay ) { foreach ( string wallpaper in wallpapers.Keys ) { maxlen = Math.Max( maxlen, wallpaper.Length ); } foreach ( string wallpaper in wallpapers.Keys ) { Console.WriteLine( "{0,-" + maxlen + "}\t{1}", wallpaper, wallpapers[wallpaper] ); if ( openwallpapers ) { Process.Start( wallpaper ); } } } else { foreach ( string wallpaper in wallpapers.Keys ) { Console.WriteLine( wallpaper ); if ( openwallpapers ) { Process.Start( wallpaper ); } } } #endregion Return Results return wallpapers.Count; } static string Bytes2Path( byte[] bytesarray ) { string imageraw = Encoding.Unicode.GetString( bytesarray ).Replace( "\0", "" ); string image = imageraw.Substring( imageraw.IndexOf( ":\\" ) - 1 ); string pattern = @"(?<=\.[a-z0-9]+)[^a-z0-9].*"; image = Regex.Replace( image, pattern, "" ); if ( File.Exists( image ) ) { return image; } return null; } static int ShowHelp( params string[] errmsg ) { #region Help Text /* GetCurrentWallpapers, Version 1.01 Get the path(s) of the current wallpaper(s) Usage: GetCurrentWallpapers [ /O ] [ /S ] [ /V ] Where: /O tells the program to open each wallpaper found /S tells the program to get only the first wallpaper it finds /V tells the program to display the registry keys for each wallpaper found (Verbose mode) Notes: Without /S the listed wallpapers may include previous ones too. Return code ("errorlevel") equals the number of wallpapers found, or -1 in case of (command line) errors. Credits: Based on PowerShell code by Ramesh Srinivasan on GitHub: https://gist.github.com/winhelponline/4dc635770d5b123f6c1a719326037880 Written by Rob van der Woude https://www.robvanderwoude.com */ #endregion Help Text #region Error Message if ( errmsg.Length > 0 ) { List errargs = new List( errmsg ); errargs.RemoveAt( 0 ); Console.Error.WriteLine( ); Console.ForegroundColor = ConsoleColor.Red; Console.Error.Write( "ERROR:\t" ); Console.ForegroundColor = ConsoleColor.White; Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) ); Console.ResetColor( ); } #endregion Error Message #region Display Help Text Console.Error.WriteLine( ); Console.Error.WriteLine( "GetCurrentWallpapers, Version {0}", progver ); Console.Error.WriteLine( "Get the path(s) of the current wallpaper(s)" ); Console.Error.WriteLine( ); Console.Error.Write( "Usage: " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.WriteLine( "GetCurrentWallpapers [ /O ] [ /S ] [ /V ]" ); Console.ResetColor( ); Console.Error.WriteLine( ); Console.Error.Write( "Where: " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "/O" ); Console.ResetColor( ); Console.Error.WriteLine( " tells the program to open each wallpaper found" ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( " /S" ); Console.ResetColor( ); Console.Error.WriteLine( " tells the program to get only the first wallpaper it finds" ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( " /V" ); Console.ResetColor( ); Console.Error.WriteLine( " tells the program to display the registry keys for each" ); Console.Error.Write( " wallpaper found (" ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "V" ); Console.ResetColor( ); Console.Error.WriteLine( "erbose mode)" ); Console.Error.WriteLine( ); Console.Error.WriteLine( "Notes: Without /S the listed wallpapers may include previous ones too." ); Console.Error.WriteLine( " Return code (\"errorlevel\") equals the number of wallpapers found," ); Console.Error.WriteLine( " or -1 in case of (command line) errors." ); Console.Error.WriteLine( ); Console.Error.WriteLine( "Credits: Based on PowerShell code by Ramesh Srinivasan on GitHub:" ); Console.ForegroundColor = ConsoleColor.DarkGray; Console.Error.WriteLine( " https://gist.github.com/winhelponline/4dc635770d5b123f6c1a719326037880" ); Console.ResetColor( ); Console.Error.WriteLine( ); Console.Error.WriteLine( "Written by Rob van der Woude" ); Console.Error.WriteLine( "https://www.robvanderwoude.com" ); #endregion Display Help Text return -1; } } }