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);
}
}
查看全文
相关阅读:
Python+fiddler(基于Cookie绕过验证码自动登录)
Python+selenium(警告框处理)
Python+selenium(多表单、多窗口切换)
Python+selenium(定位一组元素)
Python+selenium登录测试
【转载】python format遇上花括号{}
【转载】判断当前使用的编译器及操作系统
动态库的创建,隐式加载和显式加载
Google C++单元测试框架GoogleTest---AdvancedGuide(译文)
三次样条插值 cubic spline interpolation
原文地址:https://www.cnblogs.com/ami/p/455707.html
最新文章
manjaro_pam修复记录.md
磁盘查看与更改.md
gdb使用教程.md
linux_cmd.md
分辨率+终端字体.md
Python 面向对象(初级篇)
python-面向对象编程
抓包工具Charles使用
python1:基础数据类型(上)
阿拉伯数字金额转中文大写 (python实现)
热门文章
Python基础之--from __future__ import unicode_literals作用
adb命令
jmeter分布式测试
性能测试开始前的准备工作
jmeter做dubbo接口测试
unittest断言方法的使用
Pychorm提示Unresolved reference 导入模块报错
Python+selenium(Autolt实现上传)
Python+selenium(操作隐藏元素)
Python+selenium常用方法(Webdriver API)
Copyright © 2011-2022 走看看