Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Ionic.Zip;
- using System;
- using System.ComponentModel;
- using System.Diagnostics;
- using System.IO;
- using System.Net;
- using System.Threading;
- using System.Windows.Forms;
- namespace AutoUpdater
- {
- public partial class MainForm : Form
- {
- public string tempFileName = Environment.CurrentDirectory + @"\update.temp";
- public MainForm()
- {
- InitializeComponent();
- progressTracker.Hide();
- labelCheckingVersion.Dock = DockStyle.Fill;
- }
- private void MainForm_Load(object sender, EventArgs e)
- {
- if(File.Exists(tempFileName))
- {
- File.Delete(tempFileName);
- }
- if(CheckVersion())
- {
- StartHesaBot();
- }
- else
- {
- labelCheckingVersion.Hide();
- progressTracker.Dock = DockStyle.Fill;
- progressTracker.Show();
- progressTracker.SetStatus("Downloading Hesa Bot");
- StartDownload();
- }
- }
- private bool CheckVersion()
- {
- try
- {
- var versionInfo = FileVersionInfo.GetVersionInfo("Hesa Bot.exe");
- string version = versionInfo.ProductVersion;
- //MessageBox.Show(version, "Hesa Bot");
- using (WebClient webClient = new WebClient())
- {
- webClient.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.BypassCache);
- webClient.Headers.Add("Cache-Control", "no-cache");
- var remoteVersion = webClient.DownloadString("https://hesabot.com/version.txt");
- if (version != remoteVersion)
- {
- return false;
- }else
- {
- return true;
- }
- }
- }
- catch(Exception ex)
- {
- MessageBox.Show("Cannot verify version, will close.", "Hesa Bot");
- Environment.Exit(0);
- }
- return false;
- }
- private void StartHesaBot()
- {
- if (PlatformController.GetPlatform() != Platform.Windows)
- {
- Process.Start("mono \"" + Environment.CurrentDirectory + "\\Hesa Bot.exe\"", "HesaBotStartedFromUpdater");
- }
- else
- Process.Start(Environment.CurrentDirectory + "\\Hesa Bot.exe", "HesaBotStartedFromUpdater");
- Thread.Sleep(1000);
- Environment.Exit(0);
- }
- private void StartDownload()
- {
- if (File.Exists(tempFileName))
- {
- File.Delete(tempFileName);
- }
- Thread thread = new Thread(() => {
- using (WebClient client = new WebClient())
- {
- client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
- client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
- client.DownloadFileAsync(new Uri("https://hesabot.com/update.zip"), tempFileName);
- }
- });
- thread.Start();
- }
- void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
- {
- BeginInvoke((MethodInvoker)delegate {
- double bytesIn = double.Parse(e.BytesReceived.ToString());
- double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
- double percentage = bytesIn / totalBytes * 100;
- progressTracker.SetProgress("Downloaded " + (e.BytesReceived / 1000 / 1000) + "MB of " + (e.TotalBytesToReceive / 1000 / 1000) + "MB");
- progressTracker.SetPercent(int.Parse(Math.Truncate(percentage).ToString()));
- });
- }
- void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
- {
- BeginInvoke((MethodInvoker)delegate {
- progressTracker.SetStatus("Download completed");
- });
- Thread.Sleep(1000);
- BeginInvoke((MethodInvoker)delegate {
- progressTracker.SetStatus("Installing update");
- progressTracker.SetProgress("Please wait.");
- });
- try
- {
- if (File.Exists(tempFileName))
- {
- try
- {
- foreach (var process in Process.GetProcessesByName("Hesa Bot"))
- {
- process.Kill();
- }
- foreach (var process in Process.GetProcessesByName("Hesa Bot Server"))
- {
- process.Kill();
- }
- }
- catch(Exception)
- {
- }
- using (ZipFile zip = ZipFile.Read(tempFileName))
- {
- zip.ExtractProgress += new EventHandler<ExtractProgressEventArgs>(zip_ExtractProgress);
- zip.ExtractAll(Environment.CurrentDirectory + "\\", ExtractExistingFileAction.OverwriteSilently);
- }
- }
- }
- catch(Exception ex)
- {
- MessageBox.Show(this, ex.StackTrace);
- }
- }
- void zip_ExtractProgress(object sender, ExtractProgressEventArgs e)
- {
- try
- {
- if (e.TotalBytesToTransfer > 0)
- {
- var percent = Convert.ToInt32(100 * e.BytesTransferred / e.TotalBytesToTransfer);
- BeginInvoke((MethodInvoker)delegate {
- progressTracker.SetPercent(percent);
- progressTracker.SetProgress("Installing (" + (e.BytesTransferred / 1000 / 1000) + "MB of " + (e.TotalBytesToTransfer / 1000 / 1000) + "MB)");
- });
- }
- if (e.EventType == ZipProgressEventType.Extracting_AfterExtractAll)
- {
- BeginInvoke((MethodInvoker)delegate {
- progressTracker.SetStatus("Installation completed");
- });
- Thread.Sleep(1000);
- //File.Delete(tempFileName);
- StartHesaBot();
- }
- }
- catch (Exception ex)
- {
- //MessageBox.Show(this, ex.StackTrace);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement