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);
}
}
查看全文
相关阅读:
zabbix_agent 主动模式配置
zabbix 监控ipmi
超级详细全截图化VMware 安装ubantu
docker 部署
C# DataTable和List转换操作类
C#类型转换工具类
C# 注册windows 服务
C# wsdl.exe 生成类文件
visual studio code download url
c# xml序列化和反序列化
原文地址:https://www.cnblogs.com/ami/p/455707.html
最新文章
js ==与===区别
移位运算符
spring @Value 设置默认值
JPA 的 CascadeType 属性 和 FetchType属性 和 各种映射关系
docker命令汇总
Docker 私有仓库建立(加密和用户验证)
搭建docker私有仓库(https)
创建docker公共仓库
搭建docker私有仓库
免密登录和免密远程复制
热门文章
在线安全工具
基于Dockerfile创建docker镜像
安全漏洞top10
消息摘要、哈希(hash)、加盐
mangodb与mysql的区别及部署
zabbix 监控 tomcat
jdk监控tomcat
zabbix web url监控
zabbix-proxy 层级制监控
zabbix 另一种方式取 zabbix-sender
Copyright © 2011-2022 走看看