-
Notifications
You must be signed in to change notification settings - Fork 1
/
Updater.cs
89 lines (80 loc) · 2.98 KB
/
Updater.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
namespace ElectricShimmer
{
class Updater
{
private static Github electricShimmer = new Github("https://api.github.com/repos/Dr-Electron/ElectricShimmer/releases");
public static bool CheckUpdate()
{
try
{
Version Local_Version = Assembly.GetExecutingAssembly().GetName().Version;
Version Github_Version = Version.Parse(electricShimmer.GetReleasebyVersion().tag_name.Remove(0, 1));
if (Github_Version.CompareTo(Local_Version) > 0)
return true;
return false;
}
catch (Exception exc)
{
Log.Write(exc.Message, LogLevel.EXCEPTION);
return false;
}
}
public static void Update()
{
try
{
GithubObjects.Releases.Assets app = electricShimmer.GetReleasebyVersion().assets.Where(x => x.name.Contains(".exe")).ToList()[0];
string download_url = app.browser_download_url;
Directory.CreateDirectory("temp");
using (WebClient wc = new WebClient())
{
wc.DownloadProgressChanged += ((MainWindow)App.Current.MainWindow).DownloadProgressChanged;
wc.DownloadFileCompleted += ((MainWindow)App.Current.MainWindow).DownloadFinished;
wc.DownloadFileAsync(
new Uri(download_url),
@"temp\" + app.name
);
}
}
catch (Exception exc)
{
Log.Write(exc.Message, LogLevel.EXCEPTION);
}
}
public static void FinishingUpdate()
{
try
{
// Change the currently running executable so it can be overwritten.
Process thisprocess = Process.GetCurrentProcess();
string me = thisprocess.MainModule.FileName;
string bak = me + ".bak";
if (File.Exists(bak))
File.Delete(bak);
File.Move(me, bak);
File.Copy(bak, me);
var directory = new DirectoryInfo("temp");
var files = directory.GetFiles("*.exe", SearchOption.AllDirectories);
string destination = files[0].FullName.Replace(directory.FullName + @"\", "");
files[0].CopyTo(destination, true);
// Clean up.
Directory.Delete("temp", true);
// Restart.
Process.Start(me);
thisprocess.CloseMainWindow();
thisprocess.Close();
thisprocess.Dispose();
}
catch (Exception exc)
{
Log.Write(exc.Message, LogLevel.EXCEPTION);
}
}
}
}