Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Diagnostics;
- using System.IO;
- using System.Threading;
- using System.Xml.Linq;
- using System.Xml;
- namespace imgloader
- {
- class Program
- {
- static void Main(string[] args)
- {
- const string quote = "\""; //define quote for use in strings
- //define path to settings file (Will be placed in same dir as imgload.exe)
- string settingsfilepath = Path.Combine(Directory.GetCurrentDirectory(), "settings.xml");
- //Check if settings file allready exists
- if (!File.Exists(settingsfilepath))
- {
- //generate new settings.xml
- Console.WriteLine("Settings not found. Generating new!");
- Thread.Sleep(2000);
- new XDocument(
- new XElement("Settings",
- new XElement("amount_of_pictures", "0"),
- new XElement("current_image_folder_name", "active"),
- new XElement("current_image_file_name", "lockscreen.jpg"),
- new XElement("source_images_folder_name", "pictures")
- ))
- .Save("settings.xml");
- }
- //define setting string as empty before read from XML
- string amount_of_pictures = "";
- string current_image_folder_name = "";
- string current_image_file_name = "";
- string source_images_folder_name = "";
- //read settings.xml from same folder as exe
- XmlDocument document = new XmlDocument();
- document.Load(Path.Combine(Environment.CurrentDirectory, "settings.xml"));
- XmlNode node = document.SelectSingleNode("/Settings");
- //sets strings as text from xml
- amount_of_pictures = node["amount_of_pictures"].InnerText;
- current_image_folder_name = node["current_image_folder_name"].InnerText;
- current_image_file_name = node["current_image_file_name"].InnerText;
- source_images_folder_name = node["source_images_folder_name"].InnerText;
- //write settings to console
- Console.WriteLine("amount_of_pictures = " + amount_of_pictures);
- Console.WriteLine("current_image_folder_name = " + current_image_folder_name);
- Console.WriteLine("current_image_file_name = " + current_image_file_name);
- Console.WriteLine("source_images_folder_name = " + source_images_folder_name);
- string source_images_folder_name_full_path = System.Environment.CurrentDirectory + "\\" + source_images_folder_name;
- string current_image_folder_name_full_path = System.Environment.CurrentDirectory + "\\" + current_image_folder_name;
- int amount_of_pictures_int_low = int.Parse(amount_of_pictures);
- int amount_of_pictures_int = amount_of_pictures_int_low + 1;
- Console.WriteLine("");
- //check if readme.txt and lastactive.txt exists. If not, create them
- string textFile = Path.Combine(Directory.GetCurrentDirectory(), "lastactive.txt");
- string readmefile = Path.Combine(Directory.GetCurrentDirectory(), "readme.txt");
- if (!File.Exists(textFile))
- {
- using (var tw = new StreamWriter(textFile, true))
- {
- tw.WriteLine("0");
- }
- }
- if (!File.Exists(readmefile))
- {
- using (var tw = new StreamWriter(readmefile, true))
- {
- //readme.txt text
- tw.WriteLine("App title - imgload");
- tw.WriteLine("Author: Malte Hansen");
- tw.WriteLine("Contact: malte@kkwindsolutions.com");
- tw.WriteLine("Source code: https://pastebin.com/tiRtEfDC");
- tw.WriteLine("");
- tw.WriteLine("Info:");
- tw.WriteLine("To use in task scheduler, remember to set " + quote + "start in" + quote + " as the folder where the imgload.exe is located");
- tw.WriteLine("Each picture must be named like this: https://i.imgur.com/3JDb4my.png");
- tw.WriteLine("");
- tw.WriteLine("Errorcodes:");
- tw.WriteLine("0x1 - No pictures found in source folder");
- tw.WriteLine("0x2 - Settings file not confugured (amount_of_pictures is 0)");
- tw.WriteLine("Source image was not copied to active folder.");
- tw.WriteLine("0x4 - Error in generating random number. " + quote + "amount_of_pictures" + quote + " is less than 1");
- }
- }
- // create source fodler if it does not exist
- if (!Directory.Exists(source_images_folder_name_full_path))
- {
- Directory.CreateDirectory(source_images_folder_name_full_path);
- }
- //create active folder if it does not exist
- if (!Directory.Exists(current_image_folder_name_full_path))
- {
- Directory.CreateDirectory(current_image_folder_name_full_path);
- }
- //check if source picture folder is empty
- if (!Directory.EnumerateFileSystemEntries(source_images_folder_name_full_path).Any())
- {
- //give error and exit if empty
- Console.WriteLine("Error. No files pictures found in " + source_images_folder_name_full_path);
- Thread.Sleep(2000);
- Environment.ExitCode = 1;
- Environment.Exit(1);
- }
- //check if amount_of_pictures is set to 0 in settings.xml
- if (amount_of_pictures_int_low == 0)
- {
- //give error and exit if set to 0
- Console.WriteLine("Please config before use");
- Thread.Sleep(2000);
- Environment.ExitCode = 2;
- Environment.Exit(2);
- }
- restart:
- //Generates random number
- Random rnd = new Random();
- int rndnumber = 0;
- try
- {
- rndnumber = rnd.Next(1, amount_of_pictures_int);
- }
- catch (Exception e)
- {
- //give error if cannot generate number.
- Console.WriteLine("The process failed: {0}", e.ToString());
- Console.WriteLine("");
- Console.WriteLine("Amount_of_pictures cannot be less than 1");
- Thread.Sleep(2000);
- Environment.ExitCode = 4;
- Environment.Exit(4);
- }
- //convert number to string so it can be compared to lastactive.txt
- string number = rndnumber.ToString();
- Console.WriteLine("Current number: " + number);
- // Read entire text file content in one string
- string text = File.ReadAllText(textFile);
- Console.WriteLine("Last number: " + text);
- //defines text to write to lastactive.txt
- string createText = number;
- File.WriteAllText(textFile, createText);
- //if random number is same as last number, generate new number again
- if (number == text)
- {
- Thread.Sleep(500);
- goto restart;
- }
- else
- {
- //get path to avtive folder
- System.IO.DirectoryInfo di = new DirectoryInfo(current_image_folder_name_full_path);
- //delete all files in active folder
- foreach (FileInfo file in di.GetFiles())
- {
- file.Delete();
- }
- foreach (DirectoryInfo dir in di.GetDirectories())
- {
- dir.Delete(true);
- }
- //Copy x.jpg to active and name copied file "lockscreen.jpg"
- try
- {
- File.Copy(source_images_folder_name_full_path + "\\" + number + ".jpg", current_image_folder_name_full_path + "\\" + current_image_file_name);
- }
- catch (Exception e)
- {
- //give error if cannot copy file
- Console.WriteLine("Error 0x3: The process failed: {0}", e.ToString());
- Thread.Sleep(2000);
- Environment.ExitCode = 3;
- Environment.Exit(3);
- }
- Thread.Sleep(2000);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement