Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.78 KB | None | 0 0
  1. using Ionic.Zip;
  2. using System;
  3. using System.ComponentModel;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Net;
  7. using System.Threading;
  8. using System.Windows.Forms;
  9.  
  10. namespace AutoUpdater
  11. {
  12.     public partial class MainForm : Form
  13.     {
  14.         public string tempFileName = Environment.CurrentDirectory + @"\update.temp";
  15.         public MainForm()
  16.         {
  17.             InitializeComponent();
  18.             progressTracker.Hide();
  19.             labelCheckingVersion.Dock = DockStyle.Fill;
  20.         }
  21.  
  22.         private void MainForm_Load(object sender, EventArgs e)
  23.         {
  24.             if(File.Exists(tempFileName))
  25.             {
  26.                 File.Delete(tempFileName);
  27.             }
  28.             if(CheckVersion())
  29.             {
  30.                 StartHesaBot();
  31.             }
  32.             else
  33.             {
  34.                 labelCheckingVersion.Hide();
  35.                 progressTracker.Dock = DockStyle.Fill;
  36.                 progressTracker.Show();
  37.                 progressTracker.SetStatus("Downloading Hesa Bot");
  38.                 StartDownload();
  39.             }
  40.         }
  41.  
  42.         private bool CheckVersion()
  43.         {
  44.             try
  45.             {
  46.                 var versionInfo = FileVersionInfo.GetVersionInfo("Hesa Bot.exe");
  47.                 string version = versionInfo.ProductVersion;
  48.                 //MessageBox.Show(version, "Hesa Bot");
  49.                 using (WebClient webClient = new WebClient())
  50.                 {
  51.                     webClient.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.BypassCache);
  52.                     webClient.Headers.Add("Cache-Control", "no-cache");
  53.                     var remoteVersion = webClient.DownloadString("https://hesabot.com/version.txt");
  54.  
  55.                     if (version != remoteVersion)
  56.                     {
  57.                         return false;
  58.                     }else
  59.                     {
  60.                         return true;
  61.                     }
  62.                 }
  63.             }
  64.             catch(Exception ex)
  65.             {
  66.                 MessageBox.Show("Cannot verify version, will close.", "Hesa Bot");
  67.                 Environment.Exit(0);
  68.             }
  69.             return false;
  70.         }
  71.  
  72.         private void StartHesaBot()
  73.         {
  74.             if (PlatformController.GetPlatform() != Platform.Windows)
  75.             {
  76.                 Process.Start("mono \"" + Environment.CurrentDirectory + "\\Hesa Bot.exe\"", "HesaBotStartedFromUpdater");
  77.             }
  78.             else
  79.                 Process.Start(Environment.CurrentDirectory + "\\Hesa Bot.exe", "HesaBotStartedFromUpdater");
  80.             Thread.Sleep(1000);
  81.             Environment.Exit(0);
  82.         }
  83.  
  84.         private void StartDownload()
  85.         {
  86.             if (File.Exists(tempFileName))
  87.             {
  88.                 File.Delete(tempFileName);
  89.             }
  90.             Thread thread = new Thread(() => {
  91.                 using (WebClient client = new WebClient())
  92.                 {
  93.                     client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
  94.                     client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
  95.                     client.DownloadFileAsync(new Uri("https://hesabot.com/update.zip"), tempFileName);
  96.                 }
  97.             });
  98.             thread.Start();
  99.         }
  100.  
  101.         void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
  102.         {
  103.             BeginInvoke((MethodInvoker)delegate {
  104.                 double bytesIn = double.Parse(e.BytesReceived.ToString());
  105.                 double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
  106.                 double percentage = bytesIn / totalBytes * 100;
  107.                 progressTracker.SetProgress("Downloaded " + (e.BytesReceived / 1000 / 1000) + "MB of " + (e.TotalBytesToReceive / 1000 / 1000) + "MB");
  108.                 progressTracker.SetPercent(int.Parse(Math.Truncate(percentage).ToString()));
  109.             });
  110.         }
  111.  
  112.         void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
  113.         {
  114.             BeginInvoke((MethodInvoker)delegate {
  115.                 progressTracker.SetStatus("Download completed");
  116.             });
  117.             Thread.Sleep(1000);
  118.             BeginInvoke((MethodInvoker)delegate {
  119.                 progressTracker.SetStatus("Installing update");
  120.                 progressTracker.SetProgress("Please wait.");
  121.             });
  122.             try
  123.             {
  124.                 if (File.Exists(tempFileName))
  125.                 {
  126.                     try
  127.                     {
  128.                         foreach (var process in Process.GetProcessesByName("Hesa Bot"))
  129.                         {
  130.                             process.Kill();
  131.                         }
  132.                         foreach (var process in Process.GetProcessesByName("Hesa Bot Server"))
  133.                         {
  134.                             process.Kill();
  135.                         }
  136.                     }
  137.                     catch(Exception)
  138.                     {
  139.                     }
  140.                     using (ZipFile zip = ZipFile.Read(tempFileName))
  141.                     {
  142.                         zip.ExtractProgress += new EventHandler<ExtractProgressEventArgs>(zip_ExtractProgress);
  143.                         zip.ExtractAll(Environment.CurrentDirectory + "\\", ExtractExistingFileAction.OverwriteSilently);
  144.                     }
  145.                 }
  146.             }
  147.             catch(Exception ex)
  148.             {
  149.                 MessageBox.Show(this, ex.StackTrace);
  150.             }
  151.         }
  152.  
  153.         void zip_ExtractProgress(object sender, ExtractProgressEventArgs e)
  154.         {
  155.             try
  156.             {
  157.                 if (e.TotalBytesToTransfer > 0)
  158.                 {
  159.                     var percent = Convert.ToInt32(100 * e.BytesTransferred / e.TotalBytesToTransfer);
  160.                     BeginInvoke((MethodInvoker)delegate {
  161.                         progressTracker.SetPercent(percent);
  162.  
  163.                         progressTracker.SetProgress("Installing (" + (e.BytesTransferred / 1000 / 1000) + "MB of " + (e.TotalBytesToTransfer / 1000 / 1000) + "MB)");
  164.                     });
  165.                 }
  166.                 if (e.EventType == ZipProgressEventType.Extracting_AfterExtractAll)
  167.                 {
  168.                     BeginInvoke((MethodInvoker)delegate {
  169.                         progressTracker.SetStatus("Installation completed");
  170.                     });
  171.                     Thread.Sleep(1000);
  172.                     //File.Delete(tempFileName);
  173.                     StartHesaBot();
  174.                 }
  175.             }
  176.             catch (Exception ex)
  177.             {
  178.                 //MessageBox.Show(this, ex.StackTrace);
  179.             }
  180.         }
  181.  
  182.     }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement