Created
September 17, 2020 17:05
-
-
Save derekantrican/1b998d90d53fda607a4d6810dc4d2479 to your computer and use it in GitHub Desktop.
C# console app to set Windows background image and lockscreen image (REQUIRES ADMIN PERMISSIONS)
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
using System; | |
using System.IO; | |
using System.Linq; | |
using System.Runtime.InteropServices; | |
using Microsoft.Win32; | |
namespace LockScreen | |
{ | |
/* ==================================================== | |
* | |
* !! MUST BE RUN AS ADMIN !! | |
* | |
* USAGE: | |
* LockScreen.exe LockScreen=C:\image.png | |
* LockScreen.exe Background=C:\image.png | |
* LockScreen.exe LockScreen=C:\image.png Background=C:\image.png | |
* ==================================================== | |
*/ | |
class Program | |
{ | |
const string REGISTRY_KEY = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\PersonalizationCSP"; | |
static void Main(string[] args) | |
{ | |
IntPtr ptr = new IntPtr(); | |
Wow64DisableWow64FsRedirection(ref ptr); | |
if (args.Any(a => a.StartsWith("LockScreen"))) | |
{ | |
string lockScreenSource = args.First(a => a.StartsWith("LockScreen")).Split(new[] { '=' }, 2)[1]; | |
bool success = SetLockScreen(lockScreenSource); | |
if (success) | |
Console.WriteLine("LockScreen set!"); | |
} | |
if (args.Any(a => a.StartsWith("Background"))) | |
{ | |
string backgroundSource = args.First(a => a.StartsWith("Background")).Split(new[] { '=' }, 2)[1]; | |
bool success = SetBackground(backgroundSource); | |
if (success) | |
Console.WriteLine("Background set!"); | |
} | |
} | |
[DllImport("kernel32.dll", SetLastError = true)] | |
public static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr); //If on 64 bit, C# will replace "System32" with "SysWOW64". This disables that. | |
private static bool SetLockScreen(string imageSource) | |
{ | |
try | |
{ | |
const string LOCK_SCREEN_DEST_PATH = @"C:\Windows\System32\LockScreen.jpg"; | |
File.Copy(imageSource, LOCK_SCREEN_DEST_PATH, true); | |
Registry.SetValue(REGISTRY_KEY, "LockScreenImageStatus", 1, RegistryValueKind.DWord); | |
Registry.SetValue(REGISTRY_KEY, "LockScreenImagePath", LOCK_SCREEN_DEST_PATH, RegistryValueKind.String); | |
Registry.SetValue(REGISTRY_KEY, "LockScreenImageUrl", LOCK_SCREEN_DEST_PATH, RegistryValueKind.String); | |
} | |
catch | |
{ | |
return false; | |
} | |
return true; | |
} | |
private static bool SetBackground(string imageSource) | |
{ | |
try | |
{ | |
const string BACKGROUND_DEST_PATH = @"C:\Windows\System32\Desktop.jpg"; | |
File.Copy(imageSource, BACKGROUND_DEST_PATH, true); | |
Registry.SetValue(REGISTRY_KEY, "DesktopImageStatus", 1, RegistryValueKind.DWord); | |
Registry.SetValue(REGISTRY_KEY, "DesktopImagePath", BACKGROUND_DEST_PATH, RegistryValueKind.String); | |
Registry.SetValue(REGISTRY_KEY, "DesktopImageUrl", BACKGROUND_DEST_PATH, RegistryValueKind.String); | |
} | |
catch | |
{ | |
return false; | |
} | |
return true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment