zoukankan
html css js c++ java
C#下实现在线升级
//
这是一个webservice
private
AppUpdate.UpdateServ UpdateSvr;
private
void
button1_Click(
object
sender, System.EventArgs e)
{
if
(LinkWebServices()
==
true
)
{
this
.label1.Text
=&
quot;连接服务器
. PASS
&
quot;;
if
(CheckVer()
==
true
)
{
this
.label2.Text
=&
quot;检查最新版本并下载
.PASS
&
quot;;
}
else
{
this
.label2.Text
=&
quot;检查最新版本并下载
.FAIL
&
quot;;
}
}
else
{
this
.label1.Text
=&
quot;连接服务器
.FAIL
&
quot;;
}
}
//
这是用来与升级服务器建立连接
private
bool
LinkWebServices()
{
try
{
UpdateSvr
=
new
UpdateServ();
return
true
;
}
catch
{
return
false
;
}
}
//
调用webservice用来检查是不是有最新的版本
private
bool
CheckVer()
{
string
path
=
Application.StartupPath;
try
{
VersionCheck(path);
return
true
;
}
catch
(Exception ex)
{
MessageBox.Show(ex.ToString());
return
false
;
}
}
private
void
VersionCheck(
string
desPath)
{
try
{
查看文件和目录
#region
查看文件和目录
if
(
!
desPath.EndsWith(@
&
quot;\
&
quot;))
desPath
+=
@
&
quot;\
&
quot;;
if
(
!
System.IO.Directory.Exists(desPath))
{
System.IO.Directory.CreateDirectory(desPath);
}
string
tempPath
=
desPath
+
@
&
quot;tempDesPathCache\
&
quot;;
if
(System.IO.Directory.Exists(tempPath))
{
System.IO.Directory.Delete(tempPath,
true
);
System.IO.Directory.CreateDirectory(tempPath);
}
else
System.IO.Directory.CreateDirectory(tempPath);
if
(
!
System.IO.File.Exists(desPath
+
&
quot;UpdateConfig.xml
&
quot;))
{
System.Xml.XmlDocument updateConfig
=
new
System.Xml.XmlDocument();
updateConfig.LoadXml(@
&
quot;
&
lt;root
&
gt;
&
lt;
/
root
&
gt;
&
quot;);
updateConfig.Save(desPath
+
&
quot;UpdateConfig.xml
&
quot;);
}
#endregion
System.Xml.XmlDocument serverXmlDoc
=
UpdateSvr.AppUpdateVertion();
System.Xml.XmlDocument localXmlDoc
=
new
System.Xml.XmlDocument();
localXmlDoc.Load(desPath
+
&
quot;UpdateConfig.xml
&
quot;);
bool
newVersionExist
=
false
;
bool
moduleExist
=
false
;
System.Xml.XmlNode serverNode0
=
serverXmlDoc.ChildNodes[
0
];
System.Xml.XmlNode localNode0
=
localXmlDoc.ChildNodes[
0
];
foreach
(System.Xml.XmlNode serverNode
in
serverNode0)
{
moduleExist
=
false
;
foreach
(System.Xml.XmlNode localNode
in
localNode0)
{
//
找到对应模块
if
(localNode.ChildNodes[
0
].InnerText
==
serverNode.ChildNodes[
0
].InnerText)
{
moduleExist
=
true
;
//
版本号判断
if
(localNode.ChildNodes[
1
].InnerText.CompareTo(serverNode.ChildNodes[
1
].InnerText)
&
lt;
0
)
{
newVersionExist
=
true
;
if
(System.Configuration.ConfigurationSettings.AppSettings[
&
quot;NetStyle
&
quot;].ToString()
==&
quot;internet
&
quot;)
{
DownloadFile(serverNode.ChildNodes[
2
].InnerText,tempPath
+
serverNode.ChildNodes[
0
].InnerText);
}
else
{
DownloadFile(serverNode.ChildNodes[
3
].InnerText,tempPath
+
serverNode.ChildNodes[
0
].InnerText);
}
}
break
;
}
}
//
没找到对应模块
if
(
false
==
moduleExist)
{
if
(System.Configuration.ConfigurationSettings.AppSettings[
&
quot;NetStyle
&
quot;].ToString()
==&
quot;internet
&
quot;)
{
DownloadFile(serverNode.ChildNodes[
2
].InnerText,tempPath
+
serverNode.ChildNodes[
0
].InnerText);
}
else
{
DownloadFile(serverNode.ChildNodes[
3
].InnerText,tempPath
+
serverNode.ChildNodes[
0
].InnerText);
}
}
}
//
写入新UpdateConfig.xml升级完毕后替换
if
(newVersionExist)
{
serverXmlDoc.Save(tempPath
+
&
quot;UpdateConfig.xml
&
quot;);
if
(DialogResult.Yes
==
MessageBox.Show(
&
quot;有新版本,是否更新
?&
quot;,
&
quot;提示
&
quot;,MessageBoxButtons.YesNo))
{
string
[] dirs
=
System.IO.Directory.GetFiles(tempPath,
&
quot;
*
.
*&
quot;);
string
fileName;
foreach
(
string
dir
in
dirs)
{
fileName
=
((dir.Split(Convert.ToChar(@
&
quot;\
&
quot;)))[dir.Split(Convert.ToChar(@
&
quot;\
&
quot;)).Length
-
1
]);
if
(System.IO.File.Exists(desPath
+
fileName))
{
//
TODO:可以支持备份以前版本
System.IO.File.Delete(desPath
+
fileName);
}
//
TODO:如果系统正在运行,您得停止系统,至于如何停止,也许可以使用System.Diagnostics.Process
System.IO.File.Move(dir,desPath
+
fileName);
}
MessageBox.Show(
&
quot;升级完毕
&
quot;);
}
else
{
//
TODO:可以支持重新提示升级
}
}
}
catch
(Exception ex)
{
throw
new
Exception(
&
quot;升级失败,原因是:
&
quot;
+
ex.Message,ex);
}
}
//
下载最新的文件
private
void
DownloadFile(
string
source,
string
fileName)
{
try
{
System.Net.WebClient myWebClient
=
new
System.Net.WebClient();
myWebClient.DownloadFile(source,fileName);
}
catch
(Exception ex)
{
throw
new
Exception(
&
quot;下载失败,原因是:
&
quot;
+
ex.Message,ex);
}
}
查看全文
相关阅读:
IOS数据持久化之归档NSKeyedArchiver
Java中导入、导出Excel
IOS开发中多线程的使用
深入分析动态管理Fragment
IOS开发之数据sqlite使用
如何在Eclipse和Tomcat的Debug过程中启用热部署
在PHP中无法连接Memcached的解决办法
Apache mod_rewrite规则重写的标志一览
Java多线程中run(), start(), join(), wait(), yield(), sleep()的使用
Centos5.8下编译安装PHP5.4和memcached, phalcon, yaf, apc
原文地址:https://www.cnblogs.com/ami/p/455707.html
最新文章
Django中的 动态URL配置
Django URL中r的意思
elipse快捷键大全 elipse快捷键详解
数据库入门级面试题(带答案) 数据库简单面试题(带答案) MySQL面试题带答案
IT行业面试指导 计算机行业面试技巧 面试技巧
The value of 'filter_horizontal[0]' must be a many-to-many field. The value of 'raw_id_fields[0]' must be a foreign key or a many-to-many field.
[转]图片中的字符分割提取(基于opencv)
试图加载格式不正确的程序。 (异常来自 HRESULT:0x8007000B)
符号(void *)何解?符号(void **)又何解??
[转]车牌识别及验证码识别的一般思路
热门文章
GMap.NET使用一
HtmlAgilityPack使用
HtmlAgilityPack使用
dotnetbar入门
[VSTS] 从零开始 Team Foundation Server 2010 安装配置详细图文教程
转]爬虫技术浅析
Openfire服务器和Spark客户端配置
C语言——指针
android_orm框架之greenDAO(二)
ActionBar+DrawerLayout实现网易新闻客户端首页
Copyright © 2011-2022 走看看