WinForm使用WebService自动升级_webservice 更新winform_ILOVEMSDN的博客-程序员秘密

技术标签: C#  winform  string  xml  webservice  microsoft  object  

winform程序相对web程序而言,功能更强大,编程更方便,但软件更新却相当麻烦,要到客户端一台一台地升级,面对这个实际问题,在最近的一个小项目中,本人设计了一个通过软件实现自动升级技术方案,弥补了这一缺陷,有较好的参考价值。

一、升级的好处。
长期以来,广大程序员为到底是使用Client/Server,还是使用Browser/Server结构争论不休,在这些争论当中,C/S结构的程序的可维护性差,布置困难,升级不方便,维护成本高就是一个相当重要的因素,也是那些B/S的支持者们将Client/Server结构打入地狱的一个重要原因。

现在好了,我们就在最新的基于Microsoft 的 WinForm上用WebServices来实现软件的自动升级功能。

二、升级的技术原理。
升级的原理有好几个,首先无非是将现有版本与最新版本作比较,发现最新的则提示用户是否升级。当然也有人用其它属性比较的,例如:文件大小。:) 或者更新日期。
而实现的方法呢?在VB时代,我使用的是XmlHTTP+INet控件。用XmlHTTP获取信息,用INET传输升级文件,而用一个简单的BAT文件来实现升级。

Public Sub CheckUpdate()
On Error Resume Next
Dim b As Boolean
Dim XmlHttp As Object
Set XmlHttp = CreateObject("Microsoft.XMLHttp")
XmlHttp.Open "GET", "Http://mu.5inet.net/MuAdmin/update.xml", False
XmlHttp.Send

Dim vs As String
vs = XmlHttp.responseText
If Err.Number > 0 Then
Exit Sub
End If

Dim Xml As Object
Set Xml = CreateObject("Microsoft.XmlDom")
Xml.LoadXml vs
Dim Version As String
Dim downAddr As String
Dim FSize As Long
Dim fInfo As String
Version = Xml.DocumentElement.ChildNodes(0).Text
downAddr = Xml.DocumentElement.ChildNodes(1).Text
FSize = CLng(Xml.DocumentElement.ChildNodes(2).Text)
fInfo = Xml.DocumentElement.ChildNodes(3).Text
Set Xml = Nothing
Set XmlHttp = Nothing

Dim Major As Long
Dim Minor As Long
Dim Revision As Long
Dim C() As String
C = Split(Version, ".")
Major = CLng(C(0))
Minor = CLng(C(1))
Revision = CLng(C(2))

If Major > App.Major Then
b = True
ElseIf Minor > App.Minor Then
b = True
ElseIf Revision > App.Revision Then
b = True
Else
b = False
End If
If (b) Then
Dim result As VbMsgBoxResult
result = MsgBox("发现程序新版本。当前版本为:" & App.Major & "." & App.Minor & "." & App.Revision & ",目前最新版本为:" & Version & ",是否进行更新?", vbQuestion Or vbYesNo, "自动更新")
If result = vbYes Then
Dim frm As New Update
frm.DownloadAddress = downAddr
frm.size = FSize
frm.InfoPage = fInfo
frm.Version = Version
frm.Show vbModal
End If
End If
End Sub


而BAT文件有个特性,是可以删除自己本身。下面是BAT文件的内容.
@echo off
echo
echo echo 欢迎使用无垠奇迹管理器升级向导。
echo 本次升级版本为:1.1.0。
echo 请按任意键开始升级无垠奇迹管理器... echo
echo
pause
del SQLSrvBrowser.Exe
ren ~update.tmp SQLSrvBrowser.Exe
echo 升级成功,按任意键重新启动应用程序。
pause
start http://mu.5inet.net/
start SQLSrvBrowser.Exe
del update.bat


三、在.Net时代的实现。
在.Net时代,我们就有了更多的选择,可以使用WebRequest,也可以使用WebServices。在这里我们将用WebServices来实现软件的自动升级。

实现原理:在WebServices中实现一个GetVer的WebMethod方法,其作用是获取当前的最新版本。
  然后将现在版本与最新版本比较,如果有新版本,则进行升级。

  步骤:
    1、准备一个XML文件 (Update.xml)。
<?xml version="1.0" encoding="utf-8" ?>
<product>
<version>1.0.1818.42821</version>
<descrīption>修正一些Bug</descrīption>
<filelist count="4" sourcepath="./update/">
<item name="City.xml" size="">
<value />
</item>
<item name="CustomerApplication.exe" size="">
<value />
</item>
<item name="Interop.SHDocVw.dll" size="">
<value />
</item>
<item name="Citys.xml" size="">
<value />
</item>
</filelist>
</product>
作用是作为一个升级用的模板。
    2、WebServices的GetVer方法。


[WebMethod(Descrīption="取得更新版本")]
public string GetVer()
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("update.xml"));
XmlElement root = doc.DocumentElement;
return root.SelectSingleNode("version").InnerText;
}
     3、WebServices的GetUpdateData方法。
[WebMethod(Descrīption="在线更新软件")]
[SoapHeader("sHeader")]
public System.Xml.XmlDocument GetUpdateData()
{
//验证用户是否登陆
if(sHeader==null)
return null;
if(!DataProvider.GetInstance.CheckLogin(sHeader.Username,sHeader.Password))
return null;
//取得更新的xml模板内容
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("update.xml"));
XmlElement root = doc.DocumentElement;
//看看有几个文件需要更新
XmlNode updateNode = root.SelectSingleNode("filelist");
string path = updateNode.Attributes["sourcepath"].Value;
int count = int.Parse(updateNode.Attributes["count"].Value);
//将xml中的value用实际内容替换
for(int i=0;i<count;i++)
{
XmlNode itemNode = updateNode.ChildNodes[i];
string fileName = path + itemNode.Attributes["name"].Value;
FileStream fs = File.OpenRead(Server.MapPath(fileName));
itemNode.Attributes["size"].Value = fs.Length.ToString();
BinaryReader br = new BinaryReader(fs);
//这里是文件的实际内容,使用了Base64String编码
itemNode.SelectSingleNode("value").InnerText = Convert.ToBase64String(br.ReadBytes((int)fs.Length),0,(int)fs.Length);
br.Close();
fs.Close();
}
return doc;
}
    4、在客户端进行的工作。
      首先引用此WebServices,例如命名为:WebSvs,
string nVer = Start.GetService.GetVer(); 
if(Application.ProductVersion.CompareTo(nVer)<=0)
update();

在本代码中 Start.GetService是WebSvs的一个Static 实例。首先检查版本,将结果与当前版本进行比较,如果为新版本则执行UpDate方法。 void update()
{
this.statusBarPanel1.Text = "正在 下载...";
System.Xml.XmlDocument doc = ((System.Xml.XmlDocument)Start.GetService.GetUpdateData());
doc.Save(Application.StartupPath + @"/update.xml");
System.Diagnostics.Process.Start(Application.StartupPath + @"/update.exe");
Close();
Application.Exit();
}这里为了简单起见,没有使用异步方法,当然使用异步方法能更好的提高客户体验,这个需要读者们自己去添加。:) update的作用是将升级的XML文件下载下来,保存为执行文件目录下的一个Update.xml文件。任务完成,退出程序,等待Update.Exe 来进行升级。     5、Update.Exe 的内容。 private void Form1_Load(object sender, System.EventArgs e)
{
System.Diagnostics.Process[] ps = System.Diagnostics.Process.GetProcesses();
foreach(System.Diagnostics.Process p in ps)
{
//MessageBox.Show(p.ProcessName);
if(p.ProcessName.ToLower()=="customerapplication")
{
p.Kill();
break;
}
}
XmlDocument doc = new XmlDocument();
doc.Load(Application.StartupPath + @"/update.xml");
XmlElement root = doc.DocumentElement;
XmlNode updateNode = root.SelectSingleNode("filelist");
string path = updateNode.Attributes["sourcepath"].Value;
int count = int.Parse(updateNode.Attributes["count"].Value);
for(int i=0;i<count;i++)
{
XmlNode itemNode = updateNode.ChildNodes[i];
string fileName = itemNode.Attributes["name"].Value;
FileInfo fi = new FileInfo(fileName);
fi.Delete();
//File.Delete(Application.StartupPath + @"/" + fileName);
this.label1.Text = "正在更新: " + fileName + " (" + itemNode.Attributes["size"].Value + ") ...";
FileStream fs = File.Open(fileName,FileMode.Create,FileAccess.Write);
fs.Write(System.Convert.FromBase64String(itemNode.SelectSingleNode("value").InnerText),0,int.Parse(itemNode.Attributes["size"].Value));
fs.Close();
}
label1.Text = "更新完成";
File.Delete(Application.StartupPath + @"/update.xml");
label1.Text = "正在重新启动应用程序...";
System.Diagnostics.Process.Start("CustomerApplication.exe");
Close();
Application.Exit();
} 这个代码也很容易懂,首先就是找到主进程,如果没有关闭,则用Process.Kill()来关闭主程序。然后则用一个XmlDocument来Load程序生成的update.xml文件。用xml文件里指定的路径和文件名来生成指定的文件,在这之前先前已经存在的文件删除。更新完毕后,则重新启动主应用程序。这样更新就完成了。
四、总结:
  从这个实例看来,WebService的工作是很简单的,也是很容易实现的。好好的使用WebService能够为我们的程序带来很多新的,强的功能。总而言之,.Net是易用的,强大的语言  .
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/ILOVEMSDN/article/details/1643137

智能推荐

利用Github Pages搭建个人在线简历_DilemmaVF的博客-程序员秘密

利用Github Pages搭建个人在线简历什么是 Github Pages? Github Pages 是 Github 的静态页面托管服务。它设计的初衷是为了用户能够直接通过 Github 仓库来托管用户个人、组织或是项目的专属页面。参考:https://help.github.com/articles/what-is-github-pages/可以说相当于一个可直接...

c++(5)-函数参数规则_choudan8345的博客-程序员秘密

函数默认参数规则 参数默认值从右到左提供(没有默认值的参数在有默认值参数的左边) 如果某一有默认值参数使用默认值,则后续参数均使用默认值 #include &lt;stdio.h&gt;int add(int x, int y = 0, int z = 0);int...

MySQL和PGSQL对比_pgsql和mysql在大数据量下的查询_正在输入中…………的博客-程序员秘密

MySQL和PGSQL对比比较版本:PostgreSQL 11 VS MySQL5.7(innodb引擎) Oracle官方社区版版权情况:PostgreSQL 11(免费开源)、MySQL5.7 Oracle官方社区版(免费开源)1. CPU限制 PGSQL 没有CPU核心数限制,有多少CPU核就用多少 MySQL 能用128核CPU,超过128核用不上2. 配置文件参数PGSQL 一共有255个参数,用到的大概是80个,参数比较稳定,用上个大版本配置文件

Caused by: java.lang.ClassNotFoundException: org.jboss.util.file.ArchiveBrowser$Filter_沸羊羊一个的博客-程序员秘密

java.lang.NoClassDefFoundError: org/jboss/util/file/ArchiveBrowser$Filter    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:119)    at javax.persist

【报错】pytorch DataParallel  -  StopIteration: Caught StopIteration in replica 0 on device 0._sunflower_sara的博客-程序员秘密

【报错】pytorch DataParallel - StopIteration: Caught StopIteration in replica 0 on device 0.环境:pytorch 1.5问题:pytorch单机多卡用nn.DataParallel 的时候无法forward,会报错原因:pytorch1.5的bug解决方案:降级到pytorch1.4参考文献:https://github.com/huggingface/t...

随便推点

VMware虚拟机配置文件(.vmx)损坏,提示移除,如何修复?_vmx文件损坏_喜欢吃冰棍de谷利文君的博客-程序员秘密

早起打开虚拟机,突然提示“文件损坏,是否移除?”吓我一跳,里面的文件都没有上传到云端,虽然之前有过备份,但是昨天的数据肯定是没有备份的。昨天还是好好的,怎么一打开就这样了?这种情况,我肯定不是第一个碰到的,google一下,果然有解决方法。损坏情况:.vmx文件损坏,其他文件应该都是完好的。vmware版本:15虚拟机系统是:ubuntu16.0.4所以,损坏的文件是ubuntu16.0.4.vmx修复方法:1)删除.vmx(如ubuntu16.0.4.vmx),然后新建一个同样名字的

JavaScript 中 typeof 和 instanceof 的区别_typeof和instanceof_淺蓝色嘚悲伤的博客-程序员秘密

JavaScript 中 typeof 和 instanceof 常用来判断一个变量是否为空,或者是什么类型的。

默认网关出现乱码_SpringCloudZuul服务网关_weixin_39608478的博客-程序员秘密

我们使用Eureka实现了服务注册中心来服务注册与发现。为了方便统一配置文件的集中化管理我们使用ConfigServer。而服务间通过Ribbon或Feign实现服务的消费以及均衡负载。为了使服务集群更为健壮,使用Hystrix的熔断机制来避免在微服务架构中个别服务出现异常时引起的故障蔓延。在以上架构设计中,我们的所有服务都注册到注册中心直接对外暴露使用,这样设计是否合理?(能不能加上一...

laravel安装redis扩展_weixin_33869377的博客-程序员秘密

Require 下边加"predis/predis":"^1.0"然后找同级目录下的composer.lock文件 删除Cmd进入框架根目录 执行命令 composer clearcache然后再执行 composer update最后执行 composer require predis/predis注:如果laravel是5.3版本以下进入根目录直接执行composer requi...

HDU 1272小希的迷宫 并查集_js迷宫 并查集_Tizzii的博客-程序员秘密

一、内容 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走。但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一个通道连通了房间A和B,那么既可以通过它从房间A走到房间B,也可以通过它从房间B走到房间A,为了提高难度,小希希望任意两个房间有且仅有一条路径可以相通(除非走了回头路)。小希现在把她的设计图给你,让你...

InnoDB的ib_logfile写入策略_weixin_34244102的博客-程序员秘密

2019独角兽企业重金招聘Python工程师标准&gt;&gt;&gt; ...

推荐文章

热门文章

相关标签