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);
}
}
查看全文
相关阅读:
《C++标准程序库》 第6章 STL Container
《C++语言99个常见编程错误》
单例模式
《C++标准程序库》 第7章 Iterator Adapters
Shell颜色封装(C++)
《改善C++程序的150个建议》
OpenCV之图片的创建、保存和复制
XMLDOM对象方法:对象事件
三国中最精辟的十句话
中国十大名茶及鉴别方法
原文地址:https://www.cnblogs.com/ami/p/455707.html
最新文章
【原创】反射应用一InvokeMember
【原创】WinForm导出数据到EXCEL(根据微软的Excel插件)
GeoDjango教程[3]
C语言(函数)学习之index、rindex
今天我失业了
SQL 日历
aspx.cs中
人在郁闷时
社会的规则
ssh
热门文章
策略模式
IDE
svn
《C++标准程序库》 第4章 通用工具
《UNIX网络编程》 1
《C++标准程序库》 第5章 Standard Template Library
shell
c++0x 标准新特性
简单工厂模式
c++ template
Copyright © 2011-2022 走看看