Advertisement
Guest User

imgload

a guest
Jun 26th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.65 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Diagnostics;
  7. using System.IO;
  8. using System.Threading;
  9. using System.Xml.Linq;
  10. using System.Xml;
  11.  
  12. namespace imgloader
  13. {
  14.     class Program
  15.     {
  16.  
  17.         static void Main(string[] args)
  18.         {
  19.             const string quote = "\""; //define quote for use in strings
  20.  
  21.             //define path to settings file (Will be placed in same dir as imgload.exe)
  22.             string settingsfilepath = Path.Combine(Directory.GetCurrentDirectory(), "settings.xml");
  23.             //Check if settings file allready exists
  24.             if (!File.Exists(settingsfilepath))
  25.             {
  26.                 //generate new settings.xml
  27.                 Console.WriteLine("Settings not found. Generating new!");
  28.                 Thread.Sleep(2000);
  29.                 new XDocument(
  30.             new XElement("Settings",
  31.                 new XElement("amount_of_pictures", "0"),
  32.                 new XElement("current_image_folder_name", "active"),
  33.                 new XElement("current_image_file_name", "lockscreen.jpg"),
  34.                 new XElement("source_images_folder_name", "pictures")
  35.                 ))
  36.         .Save("settings.xml");
  37.             }
  38.             //define setting string as empty before read from XML
  39.             string amount_of_pictures = "";
  40.             string current_image_folder_name = "";
  41.             string current_image_file_name = "";
  42.             string source_images_folder_name = "";
  43.  
  44.             //read settings.xml from same folder as exe
  45.             XmlDocument document = new XmlDocument();
  46.             document.Load(Path.Combine(Environment.CurrentDirectory, "settings.xml"));
  47.             XmlNode node = document.SelectSingleNode("/Settings");
  48.  
  49.             //sets strings as text from xml
  50.             amount_of_pictures = node["amount_of_pictures"].InnerText;
  51.             current_image_folder_name = node["current_image_folder_name"].InnerText;
  52.             current_image_file_name = node["current_image_file_name"].InnerText;
  53.             source_images_folder_name = node["source_images_folder_name"].InnerText;
  54.  
  55.             //write settings to console
  56.             Console.WriteLine("amount_of_pictures = " + amount_of_pictures);
  57.             Console.WriteLine("current_image_folder_name = " + current_image_folder_name);
  58.             Console.WriteLine("current_image_file_name = " + current_image_file_name);
  59.             Console.WriteLine("source_images_folder_name = " + source_images_folder_name);
  60.  
  61.  
  62.             string source_images_folder_name_full_path = System.Environment.CurrentDirectory + "\\" + source_images_folder_name;
  63.             string current_image_folder_name_full_path = System.Environment.CurrentDirectory + "\\" + current_image_folder_name;
  64.             int amount_of_pictures_int_low = int.Parse(amount_of_pictures);
  65.             int amount_of_pictures_int = amount_of_pictures_int_low + 1;
  66.             Console.WriteLine("");
  67.             //check if readme.txt and lastactive.txt exists. If not, create them
  68.             string textFile = Path.Combine(Directory.GetCurrentDirectory(), "lastactive.txt");
  69.             string readmefile = Path.Combine(Directory.GetCurrentDirectory(), "readme.txt");
  70.             if (!File.Exists(textFile))
  71.             {
  72.                 using (var tw = new StreamWriter(textFile, true))
  73.                 {
  74.                     tw.WriteLine("0");
  75.                 }
  76.             }
  77.             if (!File.Exists(readmefile))
  78.             {
  79.                 using (var tw = new StreamWriter(readmefile, true))
  80.                 {
  81.                     //readme.txt text
  82.                     tw.WriteLine("App title - imgload");
  83.                     tw.WriteLine("Author: Malte Hansen");
  84.                     tw.WriteLine("Contact: malte@kkwindsolutions.com");
  85.                     tw.WriteLine("Source code: https://pastebin.com/tiRtEfDC");
  86.                     tw.WriteLine("");
  87.                     tw.WriteLine("Info:");
  88.                     tw.WriteLine("To use in task scheduler, remember to set " + quote + "start in" + quote + " as the folder where the imgload.exe is located");
  89.                     tw.WriteLine("Each picture must be named like this: https://i.imgur.com/3JDb4my.png");
  90.                     tw.WriteLine("");
  91.                     tw.WriteLine("Errorcodes:");
  92.                     tw.WriteLine("0x1 - No pictures found in source folder");
  93.                     tw.WriteLine("0x2 - Settings file not confugured (amount_of_pictures is 0)");
  94.                     tw.WriteLine("Source image was not copied to active folder.");
  95.                     tw.WriteLine("0x4 - Error in generating random number. " + quote + "amount_of_pictures" + quote + " is less than 1");
  96.                 }
  97.             }
  98.             // create source fodler if it does not exist
  99.             if (!Directory.Exists(source_images_folder_name_full_path))
  100.             {
  101.                 Directory.CreateDirectory(source_images_folder_name_full_path);
  102.  
  103.             }
  104.             //create active folder if it does not exist
  105.             if (!Directory.Exists(current_image_folder_name_full_path))
  106.             {
  107.                 Directory.CreateDirectory(current_image_folder_name_full_path);
  108.             }
  109.  
  110.             //check if source picture folder is empty
  111.             if (!Directory.EnumerateFileSystemEntries(source_images_folder_name_full_path).Any())
  112.             {
  113.                 //give error and exit if empty
  114.                 Console.WriteLine("Error. No files pictures found in " + source_images_folder_name_full_path);
  115.                 Thread.Sleep(2000);
  116.                 Environment.ExitCode = 1;
  117.                 Environment.Exit(1);
  118.             }
  119.  
  120.             //check if amount_of_pictures is set to 0 in settings.xml
  121.             if (amount_of_pictures_int_low == 0)
  122.             {
  123.                 //give error and exit if set to 0
  124.                 Console.WriteLine("Please config before use");
  125.                 Thread.Sleep(2000);
  126.                 Environment.ExitCode = 2;
  127.                 Environment.Exit(2);
  128.             }
  129.         restart:
  130.             //Generates random number
  131.             Random rnd = new Random();
  132.             int rndnumber = 0;
  133.             try
  134.             {
  135.                 rndnumber = rnd.Next(1, amount_of_pictures_int);
  136.             }
  137.  
  138.             catch (Exception e)
  139.             {
  140.                 //give error if cannot generate number.
  141.                 Console.WriteLine("The process failed: {0}", e.ToString());
  142.                 Console.WriteLine("");
  143.                 Console.WriteLine("Amount_of_pictures cannot be less than 1");
  144.                 Thread.Sleep(2000);
  145.                 Environment.ExitCode = 4;
  146.                 Environment.Exit(4);
  147.             }
  148.  
  149.  
  150.             //convert number to string so it can be compared to lastactive.txt
  151.             string number = rndnumber.ToString();
  152.             Console.WriteLine("Current number: " + number);
  153.             // Read entire text file content in one string  
  154.             string text = File.ReadAllText(textFile);
  155.             Console.WriteLine("Last number: " + text);
  156.             //defines text to write to lastactive.txt
  157.             string createText = number;
  158.             File.WriteAllText(textFile, createText);
  159.             //if random number is same as last number, generate new number again
  160.             if (number == text)
  161.             {
  162.                 Thread.Sleep(500);
  163.                 goto restart;
  164.             }
  165.             else
  166.             {
  167.                 //get path to avtive folder
  168.                 System.IO.DirectoryInfo di = new DirectoryInfo(current_image_folder_name_full_path);
  169.                 //delete all files in active folder
  170.                 foreach (FileInfo file in di.GetFiles())
  171.                 {
  172.                     file.Delete();
  173.                 }
  174.                 foreach (DirectoryInfo dir in di.GetDirectories())
  175.                 {
  176.                     dir.Delete(true);
  177.                 }
  178.  
  179.                 //Copy x.jpg to active and name copied file "lockscreen.jpg"
  180.                 try
  181.                 {
  182.                     File.Copy(source_images_folder_name_full_path + "\\" + number + ".jpg", current_image_folder_name_full_path + "\\" + current_image_file_name);
  183.                 }
  184.  
  185.                 catch (Exception e)
  186.                 {
  187.                     //give error if cannot copy file
  188.                     Console.WriteLine("Error 0x3: The process failed: {0}", e.ToString());
  189.                     Thread.Sleep(2000);
  190.                     Environment.ExitCode = 3;
  191.                     Environment.Exit(3);
  192.                 }
  193.                 Thread.Sleep(2000);
  194.             }
  195.         }
  196.     }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement