-
-
Save anonymous/b70767bf62007e5b549e47513b540399 to your computer and use it in GitHub Desktop.
Wallpaper Changer
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
{ | |
"TTW_Max": 120, | |
"TTW_Min": 10, | |
"PicturesFolder": "<<Pictures Directory>>", | |
} |
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 Microsoft.Win32; | |
using Newtonsoft.Json.Linq; | |
using System; | |
using System.IO; | |
using System.Reflection; | |
using System.Runtime.InteropServices; | |
using System.Threading; | |
namespace Wallpaperchanger | |
{ | |
class WallpaperSetter | |
{ | |
const int SetDeskWallpaper = 20; | |
const int UpdateIniFile = 0x01; | |
const int SendWinIniChange = 0x02; | |
int TTW_MIN; | |
int TTW_MAX; | |
Random random; | |
string filelocation; | |
String currentfile = ""; | |
[DllImport("user32.dll", CharSet = CharSet.Auto)] | |
static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni); | |
public WallpaperSetter() | |
{ | |
random = new Random(); | |
getJSONSettings(); | |
} | |
public void SetWallpaper(String file) | |
{ | |
if (File.Exists(file)) | |
{ | |
//set new Wallpaper | |
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true); | |
SystemParametersInfo(SetDeskWallpaper, 0, file, UpdateIniFile | SendWinIniChange); | |
} | |
} | |
public void startTask() | |
{ | |
while (true) | |
{ | |
wait(); //waits before a new wallpaper is set | |
getJSONSettings(); //sets the new json settings | |
SetWallpaper(getRandomFile()); //sets the new wallpaper | |
} | |
} | |
public String getRandomFile() | |
{ | |
if (Directory.Exists(filelocation)) //check if filelocation exists | |
{ | |
String[] files = Directory.GetFileSystemEntries(filelocation,"*.jpg"); //get all files from the location | |
String selectedFile = files[random.Next(0, files.Length)]; //gets a random file from the location | |
if(currentfile.Equals(selectedFile) && files.Length > 1) //checks if the currently selected file is the same as the random | |
{ | |
return getRandomFile(); //calls the Method again to get a new random file | |
} | |
currentfile = selectedFile; //sets the current file to the newly selected one | |
return selectedFile; //returns the file | |
}else | |
{ | |
return ""; | |
} | |
} | |
public int getRandomWaitTime() | |
{ | |
return random.Next(TTW_MIN, TTW_MAX); //gets a random time based on the settings in the settings file | |
} | |
private void wait() | |
{ | |
Thread.Sleep(1000 * 60 * getRandomWaitTime()); //waits a random time | |
} | |
private void getJSONSettings() | |
{ | |
String currentDirectory = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); //gets the path of the current directory | |
String jsonpath = currentDirectory + "\\settings.json"; //gets the path to the settingsfile | |
if (File.Exists(jsonpath)) //checks if the settings file exists | |
{ | |
JObject settingsjson = JObject.Parse(File.ReadAllText(@"" + jsonpath)); //gets the jsonobject from the settingsfile | |
TTW_MIN = settingsjson.GetValue("TTW_Min").Value<int>(); //sets the waittime minimum | |
TTW_MAX = settingsjson.GetValue("TTW_Max").Value<int>(); //sets the waittime maximum | |
filelocation = Path.Combine(currentDirectory, settingsjson.GetValue("PicturesFolder").Value<String>()); //gets the path to the picturesFolder | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment