2023年11月26日发(作者:)

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

2010-04-12 来自:CNBLOG 字体大小:【

现在的软件都提供自动升级的功能,C#如何实现程序自动升级呢?本文就为你介绍C#

摘要:

实现程序的版本自动升级更新,并提供详细的实现代码供参考。

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

那么看看我是如何实现程序自动更新的。

直接上代码:

1 using System;

2 using c;

3 using ;

4 using tion;

5 using ;

6 using ;

7 using ;

8

9 namespace Update

10 {

11 ///

12 /// 更新完成触发的事件

13 ///

14 public delegate void UpdateState();

15 ///

16 /// 程序更新

17 ///

18 public class SoftUpdate

19 {

20

21 private string download;

22 private const string updateUrl = "/

";//升级配置的XML文件地址

23

24 #region 构造函数

25 public SoftUpdate() { }

26

27 ///

28 /// 程序更新

29 ///

34 }

35 #endregion

36

37 #region 属性

38 private string loadFile;

39 private string newVerson;

40 private string softName;

41 private bool isUpdate;

42

54

55 ///

56 /// 要检查更新的文件

57 ///

58 public string LoadFile

59 {

60 get { return loadFile; }

61 set { loadFile = value; }

74 ///

75 public string SoftName

76 {

77 get { return softName; }

78 set { softName = value; }

79 }

80

81 #endregion

82

94 ///

95 public void Update()

96 {

97 try

98 {

99 if (!isUpdate)

100 return;

101 WebClient wc = new WebClient();

102 string filename = "";

103 string exten = ing(

tIndexOf("."));

104 if (f(@"") == -1)

105 filename = "Update_" + eNameWit

112 catch

113 {

114 throw new Exception("更新出现错误,网络连接失败!

");

115 }

116 }

117

118 ///

119 /// 检查是否需要更新

120 ///

121 public void checkUpdate()

122 {

123 try {

130 if( == "Soft" && utes

["Name"].r() == r()) {

131 foreach(XmlNode xml in node) {

132 if( == "Verson")

133 newVerson = ext;

134 else

135 download = ext;

136 }

137 }

138 }

139

140 Version ver = new Version(newVerson);

149 catch(Exception ex) {

150 throw new Exception("更新出现错

误,请确认网络连接无误后重试!");

151 }

152 }

153

154 ///

155 /// 获取要更新的文件

156 ///

157 ///

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.2Verson>

5 <DownLoad>/DownLoad>

6 Soft>

7 Update>

程序更新调用方法:

1、先引用上面的DLL

2、调用方法代码 如下:

1 using System;

2 using c;

3 using entModel;

4 using ;

5 using g;

6 using ;

7 using ;

8 using ;

9 using ing;

10 using ;

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 {

31 {

32

33 Thread update = new Thread(new ThreadStart

());

34 ();

35 }

36 }

37 catch (Exception ex)