Created
September 17, 2020 17:40
-
-
Save derekantrican/6d99d2c1cdef9a456051f4b314b3a4f7 to your computer and use it in GitHub Desktop.
C# console app to set the top-most picture on a subreddit to the Windows lock screen
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.Net; | |
using System.Runtime.InteropServices; | |
using Microsoft.Win32; | |
using Newtonsoft.Json.Linq; | |
namespace SubredditLockscreen | |
{ | |
class Program | |
{ | |
/* ==================================================== | |
* | |
* !! MUST BE RUN AS ADMIN !! | |
* (Also requires a reference to Newtonsoft.Json) | |
* | |
* USAGE: | |
* SubredditLockscreen.exe /r/EarthPorn | |
* SubredditLockscreen.exe r/NaturePics/hot | |
* ==================================================== | |
*/ | |
static void Main(string[] args) | |
{ | |
string subredditArg = args[0]; | |
if (!subredditArg.Contains("reddit.com")) | |
{ | |
subredditArg = $"https://reddit.com{(subredditArg.StartsWith("/") ? "" : "/")}{subredditArg}"; | |
} | |
if (!subredditArg.EndsWith(".json")) | |
{ | |
subredditArg += ".json"; | |
} | |
string imageUrl = GetLatestImgPostFromSubreddit(subredditArg); | |
bool success = SetLockScreen(imageUrl); | |
if (success) | |
Console.WriteLine("Lockscreen image set!"); | |
} | |
private static string GetLatestImgPostFromSubreddit(string subredditUrl) | |
{ | |
JToken json; | |
using (WebClient client = new WebClient()) | |
{ | |
json = JToken.Parse(client.DownloadString(subredditUrl)); | |
} | |
JArray posts = json["data"]["children"] as JArray; | |
foreach (JObject post in posts) | |
{ | |
JToken postInfo = post["data"]; | |
bool isSelfPost = postInfo["is_self"].Value<bool>(); | |
string thumbnail = postInfo["thumbnail"].Value<string>(); | |
if (!isSelfPost && thumbnail != "default") | |
{ | |
string url = postInfo["url"].Value<string>(); | |
return url; | |
} | |
} | |
return ""; | |
} | |
[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 imageUrl) | |
{ | |
try | |
{ | |
IntPtr ptr = new IntPtr(); | |
Wow64DisableWow64FsRedirection(ref ptr); | |
const string REGISTRY_KEY = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\PersonalizationCSP"; | |
const string LOCK_SCREEN_DEST_PATH = @"C:\Windows\System32\LockScreen.jpg"; | |
if (File.Exists(LOCK_SCREEN_DEST_PATH)) | |
{ | |
File.Delete(LOCK_SCREEN_DEST_PATH); | |
} | |
using (WebClient client = new WebClient()) | |
{ | |
client.DownloadFile(imageUrl, LOCK_SCREEN_DEST_PATH); | |
} | |
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment