Last active
December 9, 2015 11:58
-
-
Save MVKozlov/9d384e93fff071811325 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://powershell.com/cs/blogs/tips/archive/2014/01/10/change-desktop-wallpaper.aspx | |
// slightly modified and converted to application | |
// How to compile it: save SetDesktopWallpaper.cs to Documents folder, start Powershell and copy and paste following commands | |
// | |
// cd ([System.Environment]::GetFolderPath('MyDocuments')) | |
// $compiler = Join-Path ([Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory()) csc.exe | |
// & $compiler /out:SetDesktopWallpaper.exe /target:winexe SetDesktopWallpaper.cs | |
// | |
// the command compile SetDesktopWallpaper.cs to SetDesktopWallpaper.exe | |
// use it as SetDesktopWallpaper.exe <path_to_wallpaper> <wallpaper_style> | |
// | |
using System; | |
using System.Runtime.InteropServices; | |
using Microsoft.Win32; | |
namespace Wallpaper | |
{ | |
public enum Style : int | |
{ | |
Center, Stretch | |
} | |
public class Setter { | |
public const int SetDesktopWallpaper = 20; | |
public const int UpdateIniFile = 0x01; | |
public const int SendWinIniChange = 0x02; | |
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] | |
private static extern int SystemParametersInfo (int uAction, int uParam, string lpvParam, int fuWinIni); | |
public static void SetWallpaper ( string path, Wallpaper.Style style ) { | |
using (RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true)) | |
{ | |
try { | |
switch( style ) | |
{ | |
case Style.Stretch : | |
key.SetValue(@"WallpaperStyle", "2") ; | |
key.SetValue(@"TileWallpaper", "0") ; | |
break; | |
case Style.Center : | |
key.SetValue(@"WallpaperStyle", "1") ; | |
key.SetValue(@"TileWallpaper", "0") ; | |
break; | |
} | |
} | |
finally { | |
key.Close(); | |
} | |
} | |
SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange ); | |
} | |
} | |
static class Program | |
{ | |
/// <summary> | |
/// The main entry point for the application. | |
/// </summary> | |
[STAThread] | |
static void Main(string[] args) | |
{ | |
string wpath = (args.Length > 0) ? args[0] : ""; | |
Wallpaper.Style style = Style.Center; | |
Enum.TryParse<Wallpaper.Style>((args.Length > 1) ? args[1] : "Center", true, out style); | |
Wallpaper.Setter.SetWallpaper(wpath, style); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment