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);
}
}
查看全文
相关阅读:
CentOS 软件安装(yum 和 rpm)
Ubuntu下的 PPPoE 拨号上网方法
Vim 去除因为 Unix 和 Windows 换行符不同带来的 ^M 问题
Python 在 Windows 下安装第三方包,报 Python 未注册的问题解决
scipy 安装错误及解决
Python 字典一个易犯的错误
Linux查看系统信息
系统更新报错--NO_PUBKEY
关于直播的技术整理2
关于直播的技术整理
原文地址:https://www.cnblogs.com/ami/p/455707.html
最新文章
SpringBoot全局异常处理
线程运行原理介绍
nmon各配置项含义介绍
主流浏览器和系统分辨率汇总、常见转义字符介绍
软件测试中需要熟悉的各类流程图和框架汇总
MySQL优化汇总
通过Jenkins调用自动部署war包及jar包到服务器上的Shell脚本
siege4压测脚本示例
ycsb模板介绍
ycsb安装和使用介绍
热门文章
phantomas参数选项
npm常用命令
Monkey安装和使用介绍
jenkins+phantomjs环境搭建及使用
GoAccess参数选项
GoAccess安装和使用介绍
Sphinx 生成 Windows 帮助文件 (.chm文件)
pip 安装指定版本软件包
Python(x,y) 的 FTP 下载地址
CentOS 安装 clang
Copyright © 2011-2022 走看看