using System; using System.IO; using System.Net; using System.Linq; using System.Threading; using System.Diagnostics; using System.Windows.Forms; using System.ComponentModel; namespace Launcher_SelfUpdater { public partial class Main : Form { public Int32 Method = 0; public Main() { InitializeComponent(); Text = String.Format("{0} Self Updater", Properties.Settings.Default.ServerName); } private void Main_Load(Object Sender, EventArgs Arguments) { if (Environment.GetCommandLineArgs().Length == 2) { Method = 1; } } private void Main_Shown(Object Sender, EventArgs Arguments) { new Thread(() => { while (Process.GetProcesses().FirstOrDefault(Name => Name.ProcessName == Properties.Settings.Default.LauncherFile.Split('.')[0]) != null) { Thread.Sleep(1000); } try { if (Method == 1) { if (File.Exists(String.Format("{0}{1}{2}", Program.LocalPath, Properties.Settings.Default.LauncherFolder, Properties.Settings.Default.UpdateFile))) { if (File.Exists(String.Format("{0}{1}", Program.LocalPath, Properties.Settings.Default.LauncherFile))) { File.Delete(String.Format("{0}{1}", Program.LocalPath, Properties.Settings.Default.LauncherFile)); } File.Move(String.Format("{0}{1}{2}", Program.LocalPath, Properties.Settings.Default.LauncherFolder, Properties.Settings.Default.UpdateFile), String.Format("{0}{1}", Program.LocalPath, Properties.Settings.Default.LauncherFile)); } Process.Start(String.Format("{0}{1}", Program.LocalPath, Properties.Settings.Default.LauncherFile)); Utility.Shutdown(); } else { WebClient DownloadLauncher = new WebClient(); DownloadLauncher.DownloadFileCompleted += DownloadLauncher_Completed; DownloadLauncher.DownloadFileAsync(new Uri(String.Format("{0}{1}{2}", Properties.Settings.Default.ServerURL, Properties.Settings.Default.ServerFolder, Properties.Settings.Default.LauncherFile)), String.Format("{0}{1}", Program.LocalPath, Properties.Settings.Default.LauncherFile)); } } catch (Exception Error) { Utility.Error(Error.Message); Utility.Shutdown(); } }).Start(); } private void DownloadLauncher_Completed(Object Sender, AsyncCompletedEventArgs Arguments) { Process.Start(String.Format("{0}{1}", Program.LocalPath, Properties.Settings.Default.LauncherFile)); Utility.Shutdown(); } } }