C#使用FTP实现客户端程序自动更新

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

1using System;
2using System.Collections.Generic;
3using System.Diagnostics;
4using System.IO;
5using ;
6using System.Threading;
7using System.Windows.Forms;
8
9namespace Update
10{
11 /**////


12 /// Description:
13 /// Author: ZhangRongHua
14 /// Create DateTime: 2009-6-21 12:25
15 /// UpdateHistory:
16 ///

17 public partial class frmUpdate : Form
18 {
19 Fields#region Fields
20
21 private const string CONFIGFILE = "update.xml";
22 private const string UPDATEDIR = "PMS";
23 private string appPath = Application.StartupPath;
24 private List errorList = new List();
25 private string locFile = String.Concat(Application.StartupPath, "\\", CONFIGFILE);
26 private string tmpUpdateFile = "TmpUpdate.xml";
27 private List updateList;
28 private string updateTmpPath = string.Concat(Path.GetTempPath(), "\\", UPDATEDIR);
29 private string url = String.Empty;
30
31 private FTP ftp = null;
32
33 #endregion
34
35 Delegates#region Delegates
36
37 public delegate void AsycDownLoadFile(string srcFile, string destFile, int i);
38
39 public delegate void ExecuteUpdateFiles(string srcPath, string destPath);
40
41 public delegate void UpdateComplete();
42
43 public delegate void UpdateUI(int i, string message);
44
45 #endregion
46
47 public event UpdateComplete OnUpdateComplete;
48
49 Constructor#region Constructor
50
51 public frmUpdate()
52 {
53 InitializeComponent();
54 OnUpdateComplete += new UpdateComplete(frmUpdate_OnUpdateComplete);
55 }
56
57 #endregion
58
59 Event Handler#region Event Handler
60
61 private void frmUpdate_Load(object sender, EventArgs e)
62 {
63 if(Directory.Exists(updateTmpPath))
64 {
65 Directory.Delete(updateTmpPath, true);
66 }
67
68 // 如果有主程序启动,则关闭
69 Process[] ps = Process.GetProcesses();
70 foreach (Process p in ps)
71 {
72 //MessageBox.Show(p.ProcessName);
73 if (p.ProcessName.ToLower() == "wat.pms.winform")
74 {
75 p.Kill();
76 break;
77 }
78 }
79
80 GetUpdateFiles();
81 }
82
83 private void frmUpdate_OnUpdateComplete()
84 {
85 ExecuteUpdateFiles dExecuteUpdateFiles = new ExecuteUpdateFiles(ExecuteUpdate);
86 Invoke(dExecuteUpdateFiles, new object[] {updateTmpPath, appPath});
87 }
88
89 private void frmUpdate_Shown(object sender, EventArgs e)
90 {
91

Thread updateThread = new Thread(new ThreadStart(DownLoadUpdateFiles));
92 updateThread.SetApartmentState(ApartmentState.STA);
93 updateThread.IsBackground = true;
94 Thread.Sleep(500);
95 updateThread.Start();
96
97
98 }
99
100 #endregion
101
102 Private Methods#region Private Methods
103
104 /**////


105 /// 将目标文件替换为本地文件
106 ///
107 /// Author:ZhangRongHua
108 /// Create DateTime: 2009-6-21 10:28
109 /// Update History:
110 ///

111 ///

112 /// The SRC file.
113 /// The dest file.
114 private void DownLoadFile(string srcFile, string destFile)
115 {
116 if(ftp == null )
117 {
118 ftp = new FTP(Updater.URL, "", er, Updater.Password);
119 }
120
121
122
123 if(!ftp.Connected)
124 {
125 MessageBox.Show("无法连接远程服务器,无法更新程序", "提示", MessageBoxButtons.OK, rmation);
126 Process.Start(Updater.MainProgram);
127 Close();
128
129 }
130
131 // 得到服务器端的配置文件
132 ftp.Get(srcFile, updateTmpPath, destFile);
133
134
135
136 }
137
138 /**////
139 /// 得到需要更新的文件清单
140 ///
141 /// Author:ZhangRongHua
142 /// Create DateTime: 2009-6-21 10:29
143 /// Update History:
144 ///

145 ///

146 private void GetUpdateFiles()
147 {
148 Updater.GetBaseInfo(locFile);
149 url = Updater.URL;
150 string svrFile = String.Concat(url, CONFIGFILE);
151
152 if (String.IsNullOrEmpty(svrFile))
153 {
154 BroadCastOnUpdateComplete();
155 return;156 }
157
158 Directory.CreateDirectory(updateTmpPath);
159 try
160 {
161 DownLoadFile(CONFIGFILE, tmpUpdateFile);
162 }
163 catch (Exception ex)
164 {
165 MessageBox.Show("无法连接远程服务器,无法更新程序", "提示", MessageBoxButtons.OK, rmation);
166 Process.Start(Updater.MainProgram);
167 Close();
168 }
169
170 updateList = Updater.GetUpdateFileList(locFile, tmpUpdateFile);
171 if (updateList == null || updateList.Count < 1)
172 {
173 BroadCastOnUpdateComplete();
174 return;
175 }
176
177 pbUpdate.Maximum = updateList.Count;

178 pbUpdate.Minimum = 0;
179 lblInfo.Text = String.Concat(updateList.Count, "个文件需要更新!");
180 lblInfo.BringToFront();
181 lblState.BringToFront();
182 lblInfo.Update();
183
184 pbUpdate.Maximum = updateList.Count;
185 pbUpdate.Minimum = 0;
186
187 for (int i = 0; i < updateList.Count; i++)
188 {
189 string file = updateList;
190 ListViewItem lvItem = new ListViewItem();
191 lvItem.Text = file;
192 lvItem.SubItems.Add(Updater.MainProgramVersion);
193 lvUpdateList.Items.Add(lvItem);
194 }
195 }
196
197 private void UpdateUIInfo(int i, string message)
198 {
199 pbUpdate.Value = i + 1;
200 lblState.Text = message;
201 lblState.Update();
202 }
203
204 private void BroadCastOnUpdateComplete()
205 {
206 if (OnUpdateComplete != null)
207 {
208 OnUpdateComplete();
209 }
210 }
211
212 private void DownLoadUpdateFiles()
213 {
214 if (updateList == null || updateList.Count < 1)
215 {
216 BroadCastOnUpdateComplete();
217 return;
218 }
219
220 try
221 {
222 UpdateUI dUpdateUI = new UpdateUI(UpdateUIInfo);
223 AsycDownLoadFile dAsycDownLoadFile = new AsycDownLoadFile(DownLoadFile);
224 for (int i = 0; i < updateList.Count; i++)
225 {
226 string file = updateList;
227 string destFile = String.Concat(updateTmpPath, "\\", file);
228 string destFileName = destFile.Substring(stIndexOf("/") + 1);
229 string srcFile = String.Concat(url, file);
230 var srcFileName = file.Trim('/');
231 destFile = destFile.Replace("/", "\\");
232 Directory.CreateDirectory(destFile.Substring(0, stIndexOf("\\")));
233 string curentFile = String.Concat("正在更新第", i + 1, "/", updateList.Count, "个", file);
234
235 Invoke(dAsycDownLoadFile, new object[] { srcFileName, srcFileName, i });
236 Thread.Sleep(50);
237 Invoke(dUpdateUI, new object[] {i, curentFile});
238 }
239 }
240 catch (Exception ex)
241 {
242 Debug.WriteLine(ex.Message);
243 }
244
245 BroadCastOnUpdateComplete();
246 }
247
248
249
250 private void CopyUpdateFiles(string srcPath, string destPath)
251 {
252 string[] files = Directory.GetFiles(srcPath);
253 for (int i = 0; i < files.Length; i

++)
254 {
255 string srcFile = files;
256 string destFile = string.Concat(destPath, "\\", Path.GetFileName(srcFile));
257 try
258 {
259 File.Copy(srcFile, destFile, true);
260 }
261 catch (System.IO.IOException ex)
262 {
263
264
265 }
266
267 }
268
269 string[] dirs = Directory.GetDirectories(srcPath);
270 for (int i = 0; i < dirs.Length; i++)
271 {
272 srcPath = dirs;
273 string tmpDestPath = String.Concat(destPath, "\\", Path.GetFileName(srcPath));
274 Directory.CreateDirectory(tmpDestPath);
275 CopyUpdateFiles(srcPath, tmpDestPath);
276 }
277 }
278
279
280
281 /**////


282 /// 更新完成后,要执行的动作(将下载的文件从临时目录复制到主目录,重启主程序)
283 ///
284 /// Author:ZhangRongHua
285 /// Create DateTime: 2009-6-21 12:25
286 /// Update History:
287 ///

288 ///

289 /// The SRC path.
290 /// The dest path.
291 private void ExecuteUpdate(string srcPath, string destPath)
292 {
293 if (errorList != null && errorList.Count < 1)
294 {
295 lblInfo.Text = "正在执行更新";
296 lblInfo.Update();
297 CopyUpdateFiles(srcPath, destPath);
298 File.Copy(tmpUpdateFile, locFile, true);
299 }
300 Process.Start(Updater.MainProgram);
301
302 Close();
303 }
304
305 private void DownLoadFile(string srcFile, string destFile, ListViewItem lvItem)
306 {
307 try
308 {
309 DownLoadFile(srcFile, destFile);
310 lvItem.SubItems.Add("Ok");
311 }
312 catch (Exception ex)
313 {
314 Debug.WriteLine(ex.Message);
315 lvItem.SubItems.Add("fail");
316 ErrorInfo errorInfo = new ErrorInfo();
317 errorInfo.File = srcFile;
318 errorInfo.ErrorLevel = ErrorLevel.Serious;
319 errorInfo.Message = ex.Message;
320 errorList.Add(errorInfo);
321 }
322 }
323
324 private void DownLoadFile(string srcFile, string destFile, int i)
325 {
326 ListViewItem lvItem = lvUpdateList.Items;
327
328 lvUpdateList.Items.EnsureVisible();
329 try
330 {
331 DownLoadFile(srcFile, destFile);
332 lvItem.SubItems.Add("Ok");
333 }
334 ca

tch (Exception ex)
335 {
336 Debug.WriteLine(ex.Message);
337 lvItem.SubItems.Add("fail");
338 ErrorInfo errorInfo = new ErrorInfo();
339 errorInfo.File = srcFile;
340 errorInfo.ErrorLevel = ErrorLevel.Serious;
341 errorInfo.Message = ex.Message;
342 errorList.Add(errorInfo);
343 MessageBox.Show(destFile);
344 }
345 }
346
347 #endregion
348 }
349}

相关文档
最新文档