-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUpdate.cs
133 lines (113 loc) · 6.17 KB
/
Update.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System;
using System.ComponentModel;
using System.Deployment.Application;
using System.Diagnostics;
using System.Net;
using System.Windows.Forms;
namespace ProSnap
{
internal static class Update
{
internal static void CheckForUpdate(Action action = null)
{
Trace.WriteLine("Attempting application update...", string.Format("Update.CheckForUpdate [{0}]", System.Threading.Thread.CurrentThread.Name));
if (Configuration.UpdateRestartRequired)
{
Trace.WriteLine("Restart flag set, no need to check for update.", string.Format("Update.CheckForUpdate [{0}]", System.Threading.Thread.CurrentThread.Name));
return;
}
//remove this when debugging the updater code. but typically it's useful for this to not run when in visual studio
if (Debugger.IsAttached)
{
if (action != null)
action();
return;
}
var bg = new BackgroundWorker();
bg.DoWork += (s, e) =>
{
if (VersionCheck())
{
UpdateCheckInfo info = null;
if (!ApplicationDeployment.IsNetworkDeployed)
{
Trace.WriteLine("Not running as ClickOnce, update not possible.", string.Format("Update.CheckForUpdate [{0}]", System.Threading.Thread.CurrentThread.Name));
return;
}
Trace.WriteLine("Identified as ClickOnce...", string.Format("Update.CheckForUpdate [{0}]", System.Threading.Thread.CurrentThread.Name));
ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment;
try
{
info = ad.CheckForDetailedUpdate();
}
catch (DeploymentDownloadException dde)
{
Trace.WriteLine(dde, string.Format("Update.DeploymentDownloadException [{0}]", System.Threading.Thread.CurrentThread.Name));
return;
}
catch (InvalidDeploymentException ide)
{
Trace.WriteLine(ide, string.Format("Update.InvalidDeploymentException [{0}]", System.Threading.Thread.CurrentThread.Name));
return;
}
catch (InvalidOperationException ioe)
{
Trace.WriteLine(ioe, string.Format("Update.InvalidOperationException [{0}]", System.Threading.Thread.CurrentThread.Name));
return;
}
if (!info.UpdateAvailable)
{
Trace.WriteLine("No update available.", string.Format("Update.CheckForUpdate [{0}]", System.Threading.Thread.CurrentThread.Name));
return;
}
Trace.WriteLine(string.Format("Update is available, required: '{0}'", info.IsUpdateRequired), string.Format("Update.CheckForUpdate [{0}]", System.Threading.Thread.CurrentThread.Name));
try
{
Trace.WriteLine("Attempting update...", string.Format("Update.CheckForUpdate [{0}]", System.Threading.Thread.CurrentThread.Name));
ad.Update();
Trace.WriteLine("Done, restart required", string.Format("Update.CheckForUpdate [{0}]", System.Threading.Thread.CurrentThread.Name));
Configuration.UpdateRestartRequired = true;
}
catch (DeploymentDownloadException dde)
{
Trace.WriteLine(dde, string.Format("Update.DeploymentDownloadException [{0}]", System.Threading.Thread.CurrentThread.Name));
}
}
};
bg.RunWorkerCompleted += (s, e) =>
{
Trace.WriteLine("Update check complete.", string.Format("Update.RunWorkerCompleted [{0}]", System.Threading.Thread.CurrentThread.Name));
if (action != null)
action();
};
bg.RunWorkerAsync();
}
private static bool VersionCheck()
{
Trace.WriteLine("Checking to see if we're running out of date...", string.Format("Update.VersionCheck [{0}]", System.Threading.Thread.CurrentThread.Name));
try
{
using (var wc = new WebClient())
{
//If you get an IOException about a registry key here, while debugging, check this: http://stackoverflow.com/a/13470833/1569
string result = wc.DownloadString(new Uri(@"http://factormystic.net/prosnap/version?v=" + Application.ProductVersion));
if (string.IsNullOrEmpty(result))
{
Trace.WriteLine("Failed: result null or empty", string.Format("Update.VersionCheck [{0}]", System.Threading.Thread.CurrentThread.Name));
return false;
}
int cr = result.IndexOf('\n');
Version Latest = new Version(cr > -1 ? result.Substring(0, cr) : result.Trim());
Trace.WriteLine(string.Format("Latest version is '{0}'; Current version is '{1}'", Latest, Application.ProductVersion), string.Format("Update.VersionCheck [{0}]", System.Threading.Thread.CurrentThread.Name));
return new Version(Application.ProductVersion).CompareTo(Latest) < 0;
}
}
catch (Exception ex)
{
//Really, swallow everything here, but log it
Trace.WriteLine(string.Format("Failed: '{0}'", ex.GetBaseException().Message), string.Format("Update.VersionCheck [{0}]", System.Threading.Thread.CurrentThread.Name));
return false;
}
}
}
}