Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for getcurrentwallpapers.cs

(view source code of getcurrentwallpapers.cs as plain text)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using Microsoft.Win32;
  9.  
  10.  
  11. namespace RobvanderWoude
  12. {
  13. 	internal class GetCurrentWallpapers
  14. 	{
  15. 		static readonly string progver = "1.01";
  16.  
  17.  
  18. 		static bool openwallpapers = false;
  19. 		static bool singlewallpaper = false;
  20. 		static bool verbosedisplay = false;
  21.  
  22.  
  23. 		static int Main( string[] args )
  24. 		{
  25. 			#region Parse Command Line
  26.  
  27. 			foreach ( string arg in args )
  28. 			{
  29. 				switch ( arg.ToUpper( ) )
  30. 				{
  31. 					case "/?":
  32. 						return ShowHelp( );
  33. 					case "/O":
  34. 						if ( openwallpapers )
  35. 						{
  36. 							return ShowHelp( "Duplicate command line switch /O" );
  37. 						}
  38. 						openwallpapers = true;
  39. 						break;
  40. 					case "/S":
  41. 						if ( singlewallpaper )
  42. 						{
  43. 							return ShowHelp( "Duplicate command line switch /S" );
  44. 						}
  45. 						singlewallpaper = true;
  46. 						break;
  47. 					case "/V":
  48. 						if ( verbosedisplay )
  49. 						{
  50. 							return ShowHelp( "Duplicate command line switch /V" );
  51. 						}
  52. 						verbosedisplay = true;
  53. 						break;
  54. 					default:
  55. 						return ShowHelp( "Invalid command line argument \"{0}\"", arg );
  56. 				}
  57. 			}
  58.  
  59. 			#endregion Parse Command Line
  60.  
  61.  
  62. 			Dictionary<string, string> wallpapers = new Dictionary<string, string>( );
  63.  
  64.  
  65. 			#region List Wallpapers
  66.  
  67. 			try
  68. 			{
  69. 				using ( RegistryKey key = Registry.CurrentUser.OpenSubKey( @"Control Panel\\Desktop" ) )
  70. 				{
  71. 					byte[] imagebytes = (byte[])key.GetValue( "TranscodedImageCache" );
  72. 					string image = Bytes2Path( imagebytes );
  73. 					if ( !wallpapers.Keys.Contains( image ) )
  74. 					{
  75. 						wallpapers.Add( image, @"HKEY_CURRENT_USER\Control Panel\Desktop\TranscodedImageCache" );
  76. 					}
  77.  
  78. 					if ( !singlewallpaper )
  79. 					{
  80. 						// Check if more cached values are available
  81. 						foreach ( string valuename in key.GetValueNames( ) )
  82. 						{
  83. 							if ( valuename.StartsWith( "TranscodedImageCache_" ) )
  84. 							{
  85. 								imagebytes = (byte[])key.GetValue( valuename );
  86. 								image = Bytes2Path( imagebytes );
  87. 								if ( image != null && !wallpapers.Keys.Contains( image ) )
  88. 								{
  89. 									wallpapers.Add( image, @"HKEY_CURRENT_USER\Control Panel\Desktop\" + valuename );
  90. 								}
  91. 							}
  92. 						}
  93. 					}
  94. 				}
  95. 			}
  96. 			catch { } // ignore errors
  97.  
  98. 			#endregion List Wallpapers
  99.  
  100.  
  101. 			#region Return Results
  102.  
  103. 			int maxlen = 0;
  104. 			if ( verbosedisplay )
  105. 			{
  106. 				foreach ( string wallpaper in wallpapers.Keys )
  107. 				{
  108. 					maxlen = Math.Max( maxlen, wallpaper.Length );
  109. 				}
  110. 				foreach ( string wallpaper in wallpapers.Keys )
  111. 				{
  112. 					Console.WriteLine( "{0,-" + maxlen + "}\t{1}", wallpaper, wallpapers[wallpaper] );
  113. 					if ( openwallpapers )
  114. 					{
  115. 						Process.Start( wallpaper );
  116. 					}
  117. 				}
  118. 			}
  119. 			else
  120. 			{
  121. 				foreach ( string wallpaper in wallpapers.Keys )
  122. 				{
  123. 					Console.WriteLine( wallpaper );
  124. 					if ( openwallpapers )
  125. 					{
  126. 						Process.Start( wallpaper );
  127. 					}
  128. 				}
  129. 			}
  130.  
  131. 			#endregion Return Results
  132.  
  133.  
  134. 			return wallpapers.Count;
  135. 		}
  136.  
  137.  
  138. 		static string Bytes2Path( byte[] bytesarray )
  139. 		{
  140. 			string imageraw = Encoding.Unicode.GetString( bytesarray ).Replace( "\0", "" );
  141. 			string image = imageraw.Substring( imageraw.IndexOf( ":\\" ) - 1 );
  142. 			string pattern = @"(?<=\.[a-z0-9]+)[^a-z0-9].*";
  143. 			image = Regex.Replace( image, pattern, "" );
  144. 			if ( File.Exists( image ) )
  145. 			{
  146. 				return image;
  147. 			}
  148. 			return null;
  149. 		}
  150.  
  151.  
  152. 		static int ShowHelp( params string[] errmsg )
  153. 		{
  154. 			#region Help Text
  155.  
  156. 			/*
  157. 			GetCurrentWallpapers,  Version 1.01
  158. 			Get the path(s) of the current wallpaper(s)
  159.  
  160. 			Usage:   GetCurrentWallpapers  [ /O ]  [ /S ]  [ /V ]
  161.  
  162. 			Where:   /O    tells the program to open each wallpaper found
  163. 			         /S    tells the program to get only the first wallpaper it finds
  164. 			         /V    tells the program to display the registry keys for each
  165. 			               wallpaper found (Verbose mode)
  166.  
  167. 			Notes:   Without /S the listed wallpapers may include previous ones too.
  168. 			         Return code ("errorlevel") equals the number of wallpapers found,
  169. 			         or -1 in case of (command line) errors.
  170.  
  171. 			Credits: Based on PowerShell code by Ramesh Srinivasan on GitHub:
  172. 			         https://gist.github.com/winhelponline/4dc635770d5b123f6c1a719326037880
  173.  
  174. 			Written by Rob van der Woude
  175. 			https://www.robvanderwoude.com
  176. 			*/
  177.  
  178. 			#endregion Help Text
  179.  
  180.  
  181. 			#region Error Message
  182.  
  183. 			if ( errmsg.Length > 0 )
  184. 			{
  185. 				List<string> errargs = new List<string>( errmsg );
  186. 				errargs.RemoveAt( 0 );
  187. 				Console.Error.WriteLine( );
  188. 				Console.ForegroundColor = ConsoleColor.Red;
  189. 				Console.Error.Write( "ERROR:\t" );
  190. 				Console.ForegroundColor = ConsoleColor.White;
  191. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  192. 				Console.ResetColor( );
  193. 			}
  194.  
  195. 			#endregion Error Message
  196.  
  197.  
  198. 			#region Display Help Text
  199.  
  200. 			Console.Error.WriteLine( );
  201.  
  202. 			Console.Error.WriteLine( "GetCurrentWallpapers,  Version {0}", progver );
  203.  
  204. 			Console.Error.WriteLine( "Get the path(s) of the current wallpaper(s)" );
  205.  
  206. 			Console.Error.WriteLine( );
  207.  
  208. 			Console.Error.Write( "Usage:   " );
  209. 			Console.ForegroundColor = ConsoleColor.White;
  210. 			Console.Error.WriteLine( "GetCurrentWallpapers  [ /O ]  [ /S ]  [ /V ]" );
  211. 			Console.ResetColor( );
  212.  
  213. 			Console.Error.WriteLine( );
  214.  
  215. 			Console.Error.Write( "Where:   " );
  216. 			Console.ForegroundColor = ConsoleColor.White;
  217. 			Console.Error.Write( "/O" );
  218. 			Console.ResetColor( );
  219. 			Console.Error.WriteLine( "    tells the program to open each wallpaper found" );
  220.  
  221. 			Console.ForegroundColor = ConsoleColor.White;
  222. 			Console.Error.Write( "         /S" );
  223. 			Console.ResetColor( );
  224. 			Console.Error.WriteLine( "    tells the program to get only the first wallpaper it finds" );
  225.  
  226. 			Console.ForegroundColor = ConsoleColor.White;
  227. 			Console.Error.Write( "         /V" );
  228. 			Console.ResetColor( );
  229. 			Console.Error.WriteLine( "    tells the program to display the registry keys for each" );
  230.  
  231. 			Console.Error.Write( "               wallpaper found (" );
  232. 			Console.ForegroundColor = ConsoleColor.White;
  233. 			Console.Error.Write( "V" );
  234. 			Console.ResetColor( );
  235. 			Console.Error.WriteLine( "erbose mode)" );
  236.  
  237. 			Console.Error.WriteLine( );
  238.  
  239. 			Console.Error.WriteLine( "Notes:   Without /S the listed wallpapers may include previous ones too." );
  240.  
  241. 			Console.Error.WriteLine( "         Return code (\"errorlevel\") equals the number of wallpapers found," );
  242.  
  243. 			Console.Error.WriteLine( "         or -1 in case of (command line) errors." );
  244.  
  245. 			Console.Error.WriteLine( );
  246.  
  247. 			Console.Error.WriteLine( "Credits: Based on PowerShell code by Ramesh Srinivasan on GitHub:" );
  248.  
  249. 			Console.ForegroundColor = ConsoleColor.DarkGray;
  250. 			Console.Error.WriteLine( "         https://gist.github.com/winhelponline/4dc635770d5b123f6c1a719326037880" );
  251. 			Console.ResetColor( );
  252.  
  253. 			Console.Error.WriteLine( );
  254.  
  255. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  256.  
  257. 			Console.Error.WriteLine( "https://www.robvanderwoude.com" );
  258.  
  259. 			#endregion Display Help Text
  260.  
  261.  
  262. 			return -1;
  263. 		}
  264. 	}
  265. }

page last modified: 2024-04-16; loaded in 0.0077 seconds