From bcd693e51423da6f96b5af238fb0dfa71dac99e6 Mon Sep 17 00:00:00 2001 From: pjb7687 <pjb7687@gmail.com> Date: Mon, 19 Oct 2015 23:32:06 +0900 Subject: [PATCH] Now updater can update itself. --- SingleUpdater/frmMain.cs | 47 ++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/SingleUpdater/frmMain.cs b/SingleUpdater/frmMain.cs index 01ac729..1f688e7 100644 --- a/SingleUpdater/frmMain.cs +++ b/SingleUpdater/frmMain.cs @@ -11,6 +11,7 @@ using System.Net; using System.IO; using System.IO.Compression; +using System.Threading; using Microsoft.Win32; namespace SingleUpdater @@ -22,37 +23,45 @@ public frmMain() InitializeComponent(); } - private string getDownloadFolderPath() + private void UpdateDownloadProgress(object sender, DownloadProgressChangedEventArgs e) { - return Registry.GetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", "{374DE290-123F-4565-9164-39C4925E467B}", String.Empty).ToString(); + progressBar1.Value = (int)(e.ProgressPercentage / 100.0 * 50 + 25); } private void tmrTimer_Tick(object sender, EventArgs e) { - Process[] pname = Process.GetProcessesByName("Single2013"); - if (pname.Length == 0) + bool isKilled = true; + foreach (Process proc in Process.GetProcesses()) { + if (proc.ProcessName == "Single2013" || proc.ProcessName.StartsWith("Single ")) + isKilled = false; + } + if (isKilled) { tmrTimer.Enabled = false; - progressBar1.Value = 25; + progressBar1.Value = 0; lblStatus.Text = "Checking Update Info..."; lblStatus.Invalidate(); lblStatus.Update(); lblStatus.Refresh(); Application.DoEvents(); WebClient c = new WebClient(); + c.DownloadProgressChanged += new DownloadProgressChangedEventHandler(UpdateDownloadProgress); try { string versioninfo = c.DownloadString("http://pjb7687.github.io/single/update.txt"); string url = versioninfo.Split('\n')[1]; - progressBar1.Value = 50; + progressBar1.Value = 25; lblStatus.Text = "Downloading file..."; lblStatus.Invalidate(); lblStatus.Update(); lblStatus.Refresh(); Application.DoEvents(); - c.DownloadFile(url, "update.zip"); - + c.DownloadFileAsync(new Uri(url), "update.zip"); + while (progressBar1.Value < 75) { + Thread.Sleep(10); + Application.DoEvents(); + } progressBar1.Value = 75; lblStatus.Text = "Decompressing downloaded file..."; lblStatus.Invalidate(); @@ -60,14 +69,12 @@ private void tmrTimer_Tick(object sender, EventArgs e) lblStatus.Refresh(); Application.DoEvents(); FileInfo fi = new FileInfo(Process.GetCurrentProcess().MainModule.FileName); + System.IO.File.Move(Application.ExecutablePath, Application.ExecutablePath + ".old"); using (ZipArchive archive = ZipFile.OpenRead("update.zip")) { foreach (ZipArchiveEntry entry in archive.Entries) { - if (!entry.FullName.EndsWith("SingleUpdater.exe", StringComparison.OrdinalIgnoreCase)) - { - entry.ExtractToFile(Path.Combine(fi.DirectoryName, entry.FullName), true); - } + entry.ExtractToFile(Path.Combine(fi.DirectoryName, entry.FullName), true); } } @@ -78,15 +85,27 @@ private void tmrTimer_Tick(object sender, EventArgs e) lblStatus.Refresh(); Application.DoEvents(); + System.IO.File.Delete("update.zip"); if (MessageBox.Show(null, "Done! Do you want to run Single?", "Update", MessageBoxButtons.YesNo) == DialogResult.Yes) Process.Start(fi.DirectoryName + "\\Single2013.exe"); - Process.GetCurrentProcess().Kill(); + + ProcessStartInfo startInfo = new ProcessStartInfo(); + startInfo.FileName = "cmd.exe"; + startInfo.Arguments = "/C choice /C Y /N /D Y /T 3 & Del \"" + Application.ExecutablePath + ".old\""; + startInfo.UseShellExecute = false; + startInfo.CreateNoWindow = true; + + Process delProc = new Process(); + delProc.StartInfo = startInfo; + delProc.Start(); + + Application.Exit(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); MessageBox.Show("An error occured while updating. Please download and install Single manually!"); - Process.GetCurrentProcess().Kill(); + Application.Exit(); } } }