Skip to content

Commit

Permalink
Now updater can update itself.
Browse files Browse the repository at this point in the history
  • Loading branch information
pjb7687 committed Oct 19, 2015
1 parent 742fd60 commit bcd693e
Showing 1 changed file with 33 additions and 14 deletions.
47 changes: 33 additions & 14 deletions SingleUpdater/frmMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Net;
using System.IO;
using System.IO.Compression;
using System.Threading;
using Microsoft.Win32;

namespace SingleUpdater
Expand All @@ -22,52 +23,58 @@ 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();
lblStatus.Update();
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);
}
}

Expand All @@ -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();
}
}
}
Expand Down

0 comments on commit bcd693e

Please sign in to comment.