C#实现程序的版本自动升级更新

我们做了程序,不免会有版本升级,这就需要程序有自动版本升级的功能。

 

1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using System.Reflection; 5 using System.IO; 6 using System.Net; 7 using System.Xml; 8 9 namespace Update 10 { 11 /// <summary> 12 /// 更新完成触发的事件 13 /// </summary> 14 public delegate void UpdateState(); 15 /// <summary> 16 /// 程序更新 17 /// </summary> 18 public class SoftUpdate 19 { 20 21 private string download; 22 private const string updateUrl = "http://www.csdn.net/update.xml";//升级配置的XML文件地址 23 24 #region 构造函数 25 public SoftUpdate() { } 26 27 /// <summary> 28 /// 程序更新 29 /// </summary> 30 /// <param name="file">要更新的文件</param> 31 public SoftUpdate(string file,string softName) { 32 this.LoadFile = file; 33 this.SoftName = softName; 34 } 35 #endregion 36 37 #region 属性 38 private string loadFile; 39 private string newVerson; 40 private string softName; 41 private bool isUpdate; 42 43 /// <summary> 44 /// 或取是否需要更新 45 /// </summary> 46 public bool IsUpdate 47 { 48 get 49 { 50 checkUpdate(); 51 return isUpdate; 52 } 53 } 54 55 /// <summary> 56 /// 要检查更新的文件 57 /// </summary> 58 public string LoadFile 59 { 60 get { return loadFile; } 61 set { loadFile = value; } 62 } 63 64 /// <summary> 65 /// 程序集新版本 66 /// </summary> 67 public string NewVerson 68 { 69 get { return newVerson; } 70 } 71 72 /// <summary> 73 /// 升级的名称 74 /// </summary> 75 public string SoftName 76 { 77 get { return softName; } 78 set { softName = value; } 79 } 80 81 #endregion 82 83 /// <summary> 84 /// 更新完成时触发的事件 85 /// </summary> 86 public event UpdateState UpdateFinish; 87 private void isFinish() { 88 if(UpdateFinish != null) 89 UpdateFinish(); 90 } 91 92 /// <summary> 93 /// 下载更新 94 /// </summary> 95 public void Update() 96 { 97 try 98 { 99 if (!isUpdate) 100 return; 101 WebClient wc = new WebClient(); 102 string filename = ""; 103 string exten = download.Substring(download.LastIndexOf(".")); 104 if (loadFile.IndexOf(@"/") == -1) 105 filename = "Update_" + Path.GetFileNameWithoutExtension(loadFile) + exten; 106 else 107 filename = Path.GetDirectoryName(loadFile) + "//Update_" + Path.GetFileNameWithoutExtension(loadFile) + exten; 108 wc.DownloadFile(download, filename); 109 wc.Dispose(); 110 isFinish(); 111 } 112 catch 113 { 114 throw new Exception("更新出现错误,网络连接失败!"); 115 } 116 } 117 118 /// <summary> 119 /// 检查是否需要更新 120 /// </summary> 121 public void checkUpdate() 122 { 123 try { 124 WebClient wc = new WebClient(); 125 Stream stream = wc.OpenRead(updateUrl); 126 XmlDocument xmlDoc = new XmlDocument(); 127 xmlDoc.Load(stream); 128 XmlNode list = xmlDoc.SelectSingleNode("Update"); 129 foreach(XmlNode node in list) { 130 if(node.Name == "Soft" && node.Attributes["Name"].Value.ToLower() == SoftName.ToLower()) { 131 foreach(XmlNode xml in node) { 132 if(xml.Name == "Verson") 133 newVerson = xml.InnerText; 134 else 135 download = xml.InnerText; 136 } 137 } 138 } 139 140 Version ver = new Version(newVerson); 141 Version verson = Assembly.LoadFrom(loadFile).GetName().Version; 142 int tm = verson.CompareTo(ver); 143 144 if(tm >= 0) 145 isUpdate = false; 146 else 147 isUpdate = true; 148 } 149 catch(Exception ex) { 150 throw new Exception("更新出现错误,请确认网络连接无误后重试!"); 151 } 152 } 153 154 /// <summary> 155 /// 获取要更新的文件 156 /// </summary> 157 /// <returns></returns> 158 public override string ToString() 159 { 160 return this.loadFile; 161 } 162 } 163 }

 

把代码编译为一个类库文件,通过程序引用就OK啦。

传入的参数已经有注释了。

下面是更新的XML文件类容,传到空间上面就可以了,得到XML文件的地址。

  
  
  
  
1 <? xml version="1.0" encoding="utf-8" ?>
2
< Update >
3
< Soft Name ="BlogWriter" >
4
< Verson > 1.0.1.2 </ Verson >
5
< DownLoad > http://www.csdn.net/BlogWrite.rar </ DownLoad >
6
</ Soft >
7
</ Update >

程序更新调用方法:

 1、先引用上面的DLL。

 2、调用方法代码 如下:

  
  
  
  
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8 using System.IO;
9 using System.Threading;
10 using System.Net;
11 using System.Xml;
12 using Update;
13
14 namespace UpdateTest
15 {
16 public partial class Form1 : Form
17 {
18 public Form1()
19 {
20 InitializeComponent();
21 checkUpdate();
22 }
23
24 public void checkUpdate()
25 {
26 SoftUpdate app = new SoftUpdate(Application.ExecutablePath, " BlogWriter " );
27 app.UpdateFinish += new UpdateState(app_UpdateFinish);
28 try
29 {
30 if (app.IsUpdate && MessageBox.Show( " 检查到新版本,是否更新? " , " Update " , MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
31 {
32
33 Thread update = new Thread( new ThreadStart(app.Update));
34 update.Start();
35 }
36 }
37 catch (Exception ex)
38 {
39 MessageBox.Show(ex.Message);
40 }
41 }
42
43 void app_UpdateFinish() {
44 MessageBox.Show( " 更新完成,请重新启动程序! " , " Update " , MessageBoxButtons.OK, MessageBoxIcon.Information);
45 }
46
47 }
48 }
转 http://www.csharpwin.com/csharpspace/10829r586.shtml

你可能感兴趣的:(thread,exception,String,Stream,C#,download)