c#实现客户端程序自动下载更新(单独程序)

首先,自己工作需要实现客户端程序的自动更新下载,下面简单介绍自己实现逻辑和遇到的一些问题及解决方法

信息基本都是通过XML文件配置的,下文本地需要更新的程序简称为主程序

实现步骤简介:

1.获取本地程序版本(主程序)与服务端版本进行比较,版本不一致则继续进行

2.停止本地程序进程(主程序)

3.通过服务端xml获取需更新文件所在文件夹路径

4.下载文件夹路径页面解析获取文件名字集合

5.循环下载文件到本地指定路径

6.完成下载,重启本地进程(如果更新程序本身是windows服务,则需要注意启动进程时的问题。参考 http://www.cnblogs.com/buli93/p/7086440.html

 

关键代码及xml信息:

1.主XML信息

1 <?xml version="1.0" encoding="utf-8" ?>
2 <UpdateInfo>
3   <ServerUrl>http://localhost:99/</ServerUrl>
4   <FileName localName="TerminalUpdate.xml" serverName="TerminalUpdateServer.xml">TerminalUpdate.xml</FileName>
5   <FileName localName="SentryUpdate.xml" serverName="SentryUpdateServer.xml">SentryUpdate.xml</FileName>
6 </UpdateInfo>
View Code

2.XML具体信息TerminalUpdate.xml,这里区分主次是为了可以通更新多个程序

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <AutoUpdater>
 3   <!--版本号-->
 4   <Version>2</Version>
 5   <!--最后更新时间-->
 6   <LastUpdateTime>2017/6/26 19:16:36</LastUpdateTime>
 7   <!--主程序名字-->
 8   <ProgramName>WindowsFormsApplication1.exe</ProgramName>
 9   <!--主程序文件夹路径-->
10   <Location>D:\test\mytest\WindowsFormsApplication1\bin\Debug\</Location>
11 </AutoUpdater>
View Code

3.服务端XML信息

1 <?xml version="1.0" encoding="utf-8" ?>
2 <AutoUpdater>
3   <!--版本号-->
4   <Version>1</Version>
5   <DownloadFolderPath>download_terminal</DownloadFolderPath>
6 </AutoUpdater>
View Code

4.停止本地进程

 1 private void StopLocalProgram(string path)
 2         {
 3             System.Diagnostics.Process[] ps = System.Diagnostics.Process.GetProcesses();
 4             foreach (var item in ps)
 5             {
 6                 try
 7                 {
 8                     var str = item.MainModule.FileName;
 9                     if (item.MainModule.FileName == path)
10                     {
11                         item.Kill();
12                     }
13                 }
14                 catch
15                 {
16 
17                 }
18             }
19         }
View Code

5.下载页面解析文件名字

 1 private List<string> GetFileNameList(string downloadPath)
 2         {
 3             List<string> strList = new List<string>();
 4             string filesFolderUrl = downloadPath;
 5             HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(filesFolderUrl);
 6             HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
 7 
 8             if (httpWebResponse.StatusCode == HttpStatusCode.OK)
 9             {
10                 Stream rs = httpWebResponse.GetResponseStream();
11                 StreamReader sr = new StreamReader(rs);
12                 StringBuilder sb = new StringBuilder();
13                 char[] read = new char[256];
14                 int count = sr.Read(read, 0, 256);
15                 while (count > 0)
16                 {
17                     sb.Append(read, 0, count);
18                     count = sr.Read(read, 0, 256);
19                 }
20                 sr.Close();
21                 httpWebResponse.Close();
22                 var ss = sb.ToString();
23                 Regex regEx = new Regex(@"<A HREF=.*?>(.*?)</A>", RegexOptions.Multiline | RegexOptions.IgnoreCase);
24                 MatchCollection matches = regEx.Matches(sb.ToString());
25                 for (int i = 0; i < matches.Count; i++)
26                 {
27                     if (i != 0)
28                     {
29                         var value = matches[i].Groups[1].Value;
30                         strList.Add(value);
31                     }
32                 }
33             }
34             return strList;
35         }
View Code

6.下载服务端文件到本地指定路径

 1 private bool DownloadFile(string downloadPath, string localPath, WebClient myWebClient)
 2         {
 3             try
 4             {
 5                 List<string> fileNameList = GetFileNameList(downloadPath);
 6                 foreach (var fileName in fileNameList)
 7                 {
 8                     string filePath = downloadPath + "/" + fileName;
 9                     myWebClient.DownloadFile(filePath, localPath + fileName);
10                 }
11                 return true;
12             }
13             catch (Exception ex)
14             {
15                 LogHandler.Handler.WriteLog(ex);
16                 return false;
17             }
18         }
View Code

7.重启进程

1 private void StratLocalProgram(string path)
2         {
3             System.Diagnostics.Process.Start(path);
4             //windows服务需要用下面的方法,可参考上面提到解决方案
5             //ApplicationLoader.PROCESS_INFORMATION procInfo;
6             //ApplicationLoader.StartProcessAndBypassUAC(path, out procInfo);
7             
8         }
View Code

至此,自动更新程序完成,可以通过自定义XML信息满足个人的不同需求。

 

posted @ 2017-06-27 19:15  _不离  阅读(790)  评论(0编辑  收藏  举报