CheckVersion.cs
8.7 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
using System;
using System.Reflection;
using System.Diagnostics;
using System.Windows.Forms;
using System.IO;
using BizFacade;
namespace App
{
/// <summary>
/// CheckVersion 的摘要说明。
/// </summary>
public class CheckVersion
{
private string _LocalMainVersion;
private string _LocalMainPublicKey;
private string _LocalMainCulture;
private string _RemoteMainVersion;
private string _RemoteMainPublicKey;
private string _RemoteMainCulture;
private string _UpdateFileUrl;
private string _CheckVersionExeLocalPath;
private bool _CanEscaladeClient = true;
private bool _SkipCheck = false;
/// <summary>
/// CheckVersion初始化函数
/// </summary>
public CheckVersion()
{
CheckUpdateSwitch();
Initialize();
}
/// <summary>
/// 开始版本比对入口函数
/// </summary>
public void StartCheck()
{
// GetUpdateFileUrl();
GetRemotingMainVersion();
}
/// <summary>
/// 获取运行时程序自身的版本,地区,公钥标识.
/// </summary>
private void Initialize()
{
try
{
Assembly Asm = Assembly.LoadFrom("App.exe");
AssemblyName AsmName = Asm.GetName();
this._LocalMainVersion = AsmName.Version.ToString();
this._LocalMainCulture = AsmName.CultureInfo.ToString();
this._LocalMainPublicKey = Convert.ToString(AsmName.GetPublicKeyToken());
}
catch
{
throw new Exception();
}
}
/// <summary>
/// 获取远端Buggit Client 版本
/// </summary>
/// <returns>版本</returns>
public string GetRemotingMainVersion()
{
try
{
UpdateSystem upws = new UpdateSystem();
this._RemoteMainVersion = upws.GetClientVersion();
}
catch (Exception e)
{
throw new Exception("无法连接到升级服务器!\n(" + e.Message + ")\n");
}
return this._RemoteMainVersion;
}
/// <summary>
/// 对本地和远端版本进行比较
/// </summary>
/// <returns>版本是否相符 0 为相符,1 为更新,2 为更旧</returns>
public int CompareVersion()
{
string f_LocalVersion = this._LocalMainVersion;
string f_RemoteVersion = this._RemoteMainVersion;
int f_LocalNum;
int f_RemoteNum;
if (f_LocalVersion == f_RemoteVersion)
{
return 0;
}
int i;
for (i = 0; i < 4; i++)
{
if (f_LocalVersion.LastIndexOf(@".") > 0)
{
f_LocalNum = int.Parse(f_LocalVersion.Substring(0, f_LocalVersion.IndexOf(@".")));
f_RemoteNum = int.Parse(f_RemoteVersion.Substring(0, f_RemoteVersion.IndexOf(@".")));
}
else
{
f_LocalNum = int.Parse(f_LocalVersion);
f_RemoteNum = int.Parse(f_RemoteVersion);
}
if (f_LocalNum != f_RemoteNum)
{
if (f_LocalNum < f_RemoteNum)
{
return 1;
}
else
{
return 2;
}
}
f_LocalVersion = f_LocalVersion.Substring(f_LocalVersion.IndexOf(@".") + 1);
f_RemoteVersion = f_RemoteVersion.Substring(f_RemoteVersion.IndexOf(@".") + 1);
}
return 0;
}
/// <summary>
/// 调用CheckVersion.exe
/// </summary>
public void CallUpdateFile()
{
string ErrorMsg = "";
try
{
string strTemp = Application.ExecutablePath;
strTemp = strTemp.Substring(0, strTemp.LastIndexOf("\\") + 1);
this._CheckVersionExeLocalPath = strTemp;
FileInfo fileinfo = new FileInfo(this._CheckVersionExeLocalPath + @"\LiveUpdate.exe");
if (!fileinfo.Exists)
{
ErrorMsg = "在当前目录找不到LiveUpdate.exe升级程序!\n";
}
Process myProcess = new Process();
myProcess.StartInfo.FileName = this._CheckVersionExeLocalPath + @"\LiveUpdate.exe";
// 传入"-S"的参数表示启动LiveUpdate.exe后,可自动开始升级过程
myProcess.StartInfo.Arguments = "-S";
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
myProcess.Start();
}
catch (Exception e)
{
throw new Exception(ErrorMsg + e.Message);
}
}
private void CheckUpdateSwitch()
{
string f_UpdateSwitch = "";
try
{
UpdateSystem upws = new UpdateSystem();
f_UpdateSwitch = upws.GetUpdateSwitch();
}
catch
{
f_UpdateSwitch = "";
}
if (f_UpdateSwitch == "True")
{
this._SkipCheck = true;
}
else
{
this._SkipCheck = false;
}
}
/// <summary>
/// 当前程序主版本
/// </summary>
public string LocalMainVersion
{
get
{
return _LocalMainVersion;
}
set
{
_LocalMainVersion = value;
}
}
/// <summary>
/// 当前程序公钥标识
/// </summary>
public string LocalMainPublicKey
{
get
{
return _LocalMainPublicKey;
}
set
{
_LocalMainPublicKey = value;
}
}
/// <summary>
/// 当前程序地区标识
/// </summary>
public string LocalMainCulture
{
get
{
return _LocalMainCulture;
}
set
{
_LocalMainCulture = value;
}
}
/// <summary>
/// 远端程序主版本
/// </summary>
public string RemoteMainVersion
{
get
{
return _RemoteMainVersion;
}
set
{
_RemoteMainVersion = value;
}
}
/// <summary>
/// 远端主程序公钥标识
/// </summary>
public string RemoteMainPublicKey
{
get
{
return _RemoteMainPublicKey;
}
set
{
_RemoteMainPublicKey = value;
}
}
/// <summary>
/// 远端程序地区标识
/// </summary>
public string RemoteMainCulture
{
get
{
return _RemoteMainCulture;
}
set
{
_RemoteMainCulture = value;
}
}
/// <summary>
/// 更新的Buggit Client URL地址
/// </summary>
public string UpdateFileUrl
{
get
{
return _UpdateFileUrl;
}
set
{
_UpdateFileUrl = value;
}
}
/// <summary>
/// 更新程序的本地相对路径
/// </summary>
public string CheckVersionExeLocalPath
{
get
{
return _CheckVersionExeLocalPath;
}
set
{
_CheckVersionExeLocalPath = value;
}
}
/// <summary>
/// 是否继续运行Buggit Client标志位
/// </summary>
public bool CanEscaladeClient
{
get
{
return _CanEscaladeClient;
}
set
{
_CanEscaladeClient = value;
}
}
/// <summary>
/// 是否略过版本检查
/// </summary>
public bool SkipCheck
{
get
{
return _SkipCheck;
}
set
{
_SkipCheck = value;
}
}
}
}