C#实现程序的版本自动升级更新Word格式.docx
- 文档编号:14278823
- 上传时间:2022-10-21
- 格式:DOCX
- 页数:12
- 大小:25.64KB
C#实现程序的版本自动升级更新Word格式.docx
《C#实现程序的版本自动升级更新Word格式.docx》由会员分享,可在线阅读,更多相关《C#实现程序的版本自动升级更新Word格式.docx(12页珍藏版)》请在冰豆网上搜索。
III程序更新
17
/summary>
18
publicclass
SoftUpdate
19{
20
21
22
update.xml"
23
24
25
26
27
28
29
30
31
32
33
privatestringdownload;
privateconststringupdateUrl="
;
//升级配置的XML文件地址
#region构造函数publicSoftUpdate(){}
///程序更新
///vparamname="
file"
>
要更新的文件<
/param>
publicSoftUpdate(stringfile,stringsoftName){this.LoadFile=file;
this.SoftName=softName;
35
#endregion
36
37
#region属性
38
privatestringloadFile;
39
privatestringnewVerson;
40
privatestringsoftName;
41
privateboolisUpdate;
42
43
III<
44
///或取是否需要更新
45
46
publicboolIsUpdate
47
{
48
get
49
50
checkUpdate();
51
returnisUpdate;
52
}
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
///要检查更新的文件
publicstringLoadFile{
get{returnloadFile;
}
set{loadFile=value;
///程序集新版本
publicstringNewVerson
get{returnnewVerson;
///升级的名称
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
publicstringSoftName
get{returnsoftName;
}set{softName=value;
///更新完成时触发的事件
publiceventUpdateStateUpdateFinish;
privatevoidisFinish(){
if(UpdateFinish!
=null)
UpdateFinish();
///下载更新
94
95
publicvoidUpdate()
96{
97
try
98{
99
if(!
isUpdate)
100
return;
101
WebClientwc=
newWebClient();
102
stringfilename="
"
103
tIndexOf("
."
stringexten=
));
:
download.Substring(download.Las
104
if(loadFile.IndexOf(@"
\"
)==-1)
105
filename=
Update_"
+Path.GetFileNameWit
houtExtension(loadFile)+exten;
106else
107filename=Path.GetDirectoryName(loadFile)
+"
\\Update_"
+Path.GetFileNameWithoutExtension(loadFile)+exten;
108wc.DownloadFile(download,filename);
109wc.Dispose();
110isFinish();
112
113
114
);
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
catch
thrownewException("
更新出现错误,网络连接失败!
///检查是否需要更新
publicvoidcheckUpdate()
try{
WebClientwc=newWebClient();
Streamstream=wc.OpenRead(updateUrl);
XmlDocumentxmlDoc=newXmlDocument();
xmlDoc.Load(stream);
XmlNodelist=xmlDoc.SelectSingleNode("
Update
foreach(XmlNodenodeinlist){
130if(node.Name=="
Soft"
&
&
node.Attributes
["
Name'
].Value.ToLower()==SoftName.ToLower()){
131
foreach(XmlNodexmlinnode){
132
if(xml.Name=="
Verson"
)
133
newVerson
=xml.lnnerText;
134
else
135
download=
xml.lnnerText;
136
137
138
139
140
Versionver=
newVersion(newVerson);
141Versionverson=Assembly.LoadFrom(loadFile).G
etName().Version;
142
inttm=
verson.CompareTo(ver);
143
144
if(tm>
=
0)
145
isUpdate=
false;
146
147
true;
150thrownewException("
更新出现错
误,请确认网络连接无误后重试!
151
152
153
154
155
获取要更新的文件
156
v/summary>
157
returnsx/returns>
158
publicoverridestringToString()
159
160
returnthis」oadFile;
161
162
163}
把代码编译为一个类库文件,通过程序引用就0K啦。
传入的参数已经有注释了。
下面是更新的XML文件类容,传到空间上面就可以了,得到XML文件的地址。
1<
?
xmlversion="
1.0"
encoding="
utf-8"
?
2<
Update>
3<
SoftNam="
BlogWriter"
4
/DownLoa>
Verson>
1.0.1.2<
/Verson>
5<
DownLoa>
http:
//www.csdn.net/BlogWrite.rar
6<
/Soft>
7<
/Update〉
程序更新调用方法:
1、先引用上面的DLL。
2、调用方法代码如下:
3usingSystem.ComponentModel;
4usingSystem.Data;
5usingSystem.Drawing;
6usingSystem.Text;
7usingSystem.Windows.Forms;
8usingSystem.10;
9usingSystem.Threading;
10usingSystem.Net;
11usingSystem.Xml;
12usingUpdate;
14namespaceUpdateTest
15{
16publicpartialclassForml:
Form
17{
18publicForm1()
19{
20InitializeComponent();
21checkUpdate();
22}
23publicvoidcheckUpdate()
24{
25SoftUpdateapp=newSoftUpdate(Application.Executa
blePath,"
);
h);
28try
29{
30if(app.IsUpdate&
MessageBox.Show("
检查到新版
本,是否更新?
"
"
Update"
MessageBoxButtons.YesNo,M
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C# 实现 程序 版本 自动 升级 更新