Advertisement
QwarkDev

launcher source

Dec 12th, 2020
888
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.08 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Net;
  5. using System.Windows.Forms;
  6.  
  7. namespace Launcher
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             var launcher = new Launcher();
  14.             launcher.RunApplication();
  15.         }
  16.  
  17.     }
  18.  
  19.     class Launcher
  20.     {
  21.         static string AppData { get { return Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); } }
  22.         const string FolderPath = "alt";
  23.  
  24.         readonly string BuildPath = $@"{AppData}\{FolderPath}\build.txt";
  25.         readonly string AppPath = $@"{AppData}\{FolderPath}\app.exe";
  26.  
  27.         readonly string GetLatestApp = "http://IP/latest-alt.exe";
  28.         readonly string GetLatestBuild = "http://IP/latest-build.txt";
  29.  
  30.         public void RunApplication()
  31.         {
  32.             if (!Directory.Exists($"{AppData}/{FolderPath}"))
  33.                 Directory.CreateDirectory($"{AppData}/{FolderPath}");
  34.  
  35.             if (UpdateAvailable())
  36.                 DownloadApplication();
  37.  
  38.             if (File.Exists(AppPath)) Process.Start(AppPath);
  39.             else MessageBox.Show("При скачивании последней версии ALT'a возникли ошибки.\nПроверьте интернет соединение");
  40.         }
  41.  
  42.         public bool UpdateAvailable()
  43.         {
  44.             if (!File.Exists(BuildPath) || !File.Exists(AppPath))
  45.                 return true;
  46.  
  47.             try {
  48.                 var serverBuild = new WebClient().DownloadString(GetLatestBuild);
  49.                 var localBuild = File.ReadAllText(BuildPath);
  50.  
  51.                 return serverBuild != localBuild;
  52.             } catch { return false; }
  53.         }
  54.  
  55.         public void DownloadApplication()
  56.         {
  57.             try {
  58.                 var client = new WebClient();
  59.  
  60.                 //File.Delete(AppPath);
  61.                 //File.Delete(BuildPath);
  62.  
  63.                 client.DownloadFile(GetLatestApp, AppPath);
  64.                 client.DownloadFile(GetLatestBuild, BuildPath);
  65.             } catch { }
  66.         }
  67.     }
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement