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

上传人:b****2 文档编号:892573 上传时间:2023-04-30 格式:DOCX 页数:15 大小:17.24KB
下载 相关 举报
C#实现程序的版本自动升级更新.docx_第1页
第1页 / 共15页
C#实现程序的版本自动升级更新.docx_第2页
第2页 / 共15页
C#实现程序的版本自动升级更新.docx_第3页
第3页 / 共15页
C#实现程序的版本自动升级更新.docx_第4页
第4页 / 共15页
C#实现程序的版本自动升级更新.docx_第5页
第5页 / 共15页
C#实现程序的版本自动升级更新.docx_第6页
第6页 / 共15页
C#实现程序的版本自动升级更新.docx_第7页
第7页 / 共15页
C#实现程序的版本自动升级更新.docx_第8页
第8页 / 共15页
C#实现程序的版本自动升级更新.docx_第9页
第9页 / 共15页
C#实现程序的版本自动升级更新.docx_第10页
第10页 / 共15页
C#实现程序的版本自动升级更新.docx_第11页
第11页 / 共15页
C#实现程序的版本自动升级更新.docx_第12页
第12页 / 共15页
C#实现程序的版本自动升级更新.docx_第13页
第13页 / 共15页
C#实现程序的版本自动升级更新.docx_第14页
第14页 / 共15页
C#实现程序的版本自动升级更新.docx_第15页
第15页 / 共15页
亲,该文档总共15页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

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

《C#实现程序的版本自动升级更新.docx》由会员分享,可在线阅读,更多相关《C#实现程序的版本自动升级更新.docx(15页珍藏版)》请在冰点文库上搜索。

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

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

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

2010-04-12 来自:

CNBLOG 字体大小:

【大 中 小】

∙摘要:

现在的软件都提供自动升级的功能,C#如何实现程序自动升级呢?

本文就为你介绍C#实现程序的版本自动升级更新,并提供详细的实现代码供参考。

-

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

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

直接上代码:

1usingSystem;

2usingSystem.Collections.Generic;

3usingSystem.Text;

4usingSystem.Reflection;

5usingSystem.IO;

6usingSystem.Net;

7usingSystem.Xml;

8

9namespaceUpdate

10{

11///

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

13///

14publicdelegatevoidUpdateState();

15///

16///程序更新

17///

18publicclassSoftUpdate

19{

20

21privatestringdownload;

22privateconststringupdateUrl="

23

24#region构造函数

25publicSoftUpdate(){}

26

27///

28///程序更新

29///

30///要更新的文件

31publicSoftUpdate(stringfile,stringsoftName){

32this.LoadFile=file;

33this.SoftName=softName;

34}

35#endregion

36

37#region属性

38privatestringloadFile;

39privatestringnewVerson;

40privatestringsoftName;

41privateboolisUpdate;

42

43///

44///或取是否需要更新

45///

46publicboolIsUpdate

47{

48get

49{

50checkUpdate();

51returnisUpdate;

52}

53}

54

55///

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

57///

58publicstringLoadFile

59{

60get{returnloadFile;}

61set{loadFile=value;}

62}

63

64///

65///程序集新版本

66///

67publicstringNewVerson

68{

69get{returnnewVerson;}

70}

71

72///

73///升级的名称

74///

75publicstringSoftName

76{

77get{returnsoftName;}

78set{softName=value;}

79}

80

81#endregion

82

83///

84///更新完成时触发的事件

85///

86publiceventUpdateStateUpdateFinish;

87privatevoidisFinish(){

88if(UpdateFinish!

=null)

89UpdateFinish();

90}

91

92///

93///下载更新

94///

95publicvoidUpdate()

96{

97try

98{

99if(!

isUpdate)

100return;

101WebClientwc=newWebClient();

102stringfilename="";

103stringexten=download.Substring(download.LastIndexOf("."));

104if(loadFile.IndexOf(@"\")==-1)

105filename="Update_"+Path.GetFileNameWithoutExtension(loadFile)+exten;

106else

107filename=Path.GetDirectoryName(loadFile)+"\\Update_"+Path.GetFileNameWithoutExtension(loadFile)+exten;

108wc.DownloadFile(download,filename);

109wc.Dispose();

110isFinish();

111}

112catch

113{

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

");

115}

116}

117

118///

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

120///

121publicvoidcheckUpdate()

122{

123try{

124WebClientwc=newWebClient();

125Streamstream=wc.OpenRead(updateUrl);

126XmlDocumentxmlDoc=newXmlDocument();

127xmlDoc.Load(stream);

128XmlNodelist=xmlDoc.SelectSingleNode("Update");

129foreach(XmlNodenodeinlist){

130if(node.Name=="Soft"&&node.Attributes["Name"].Value.ToLower()==SoftName.ToLower()){

131foreach(XmlNodexmlinnode){

132if(xml.Name=="Verson")

133newVerson=xml.InnerText;

134else

135download=xml.InnerText;

136}

137}

138}

139

140Versionver=newVersion(newVerson);

141Versionverson=Assembly.LoadFrom(loadFile).GetName().Version;

142inttm=verson.CompareTo(ver);

143

144if(tm>=0)

145isUpdate=false;

146else

147isUpdate=true;

148}

149catch(Exceptionex){

150thrownewException("更新出现错误,请确认网络连接无误后重试!

");

151}

152}

153

154///

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

156///

157///

158publicoverridestringToString()

159{

160returnthis.loadFile;

161}

162}

163}

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

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

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

1

xmlversion="1.0"encoding="utf-8"?

>

2

3

41.0.1.2

5

6

7

程序更新调用方法:

 1、先引用上面的DLL。

 2、调用方法代码如下:

1usingSystem;

2usingSystem.Collections.Generic;

3usingSystem.ComponentModel;

4usingSystem.Data;

5usingSystem.Drawing;

6usingSystem.Text;

7usingSystem.Windows.Forms;

8usingSystem.IO;

9usingSystem.Threading;

10usingSystem.Net;

11usingSystem.Xml;

12usingUpdate;

13

14namespaceUpdateTest

15{

16publicpartialclassForm1:

Form

17{

18publicForm1()

19{

20InitializeComponent();

21checkUpdate();

22}

23

24publicvoidcheckUpdate()

25{

26SoftUpdateapp=newSoftUpdate(Application.ExecutablePath,"BlogWriter");

27app.UpdateFinish+=newUpdateState(app_UpdateFinish);

28try

29{

30if(app.IsUpdate&&MessageBox.Show("检查到新版本,是否更新?

","Update",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)

31{

32

33Threadupdate=newThread(newThreadStart(app.Update));

34update.Start();

35}

36}

37catch(Exceptionex)

38{

39MessageBox.Show(ex.Message);

40}

41}

42

43voidapp_UpdateFinish(){

44MessageBox.Show("更新完成,请重新启动程序!

","Update",MessageBoxButtons.OK,MessageBoxIcon.Information);

45}

46

47}

48}

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 临时分类 > 批量上传

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2