来源:互联网

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

一、升级的好处

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

结构打入地狱的一个重要原因。现在好了,我们就在最新的基于Microsoft的WinForm上用Web Service来实现软件的自动升级功能。

二、升级的技术原理

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

三、在.Net时代的实现

在.Net时代,我们就有了更多的选择,可以使用WebRequest,也可以使用Web Service。在这里我们将用Web Service来实现软件的自动升级。实现原理:在Web Service中实现一个GetVer的WebMethod方法,其作用是获取当前的最新版本。然后将现在版本与最新版本比较,如果有新版本,则进行升级。步骤如下:

1.准备一个作为升级模板用的xml文件(update.xml)。

<?xml version="1.0" encoding="utf-8" ?>

<product>

 <version>1.0.1818.42821</version>

 <description>修正一些Bug</description>

 <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.Web Service的GetVer方法。

[WebMethod(Description="取得更新版本")]

public string GetVer()

{

XmlDocument doc = new XmlDocument();

doc.Load(Server.MapPath("update.xml"));

XmlElement root = doc.DocumentElement;

return root.SelectSingleNode("version").InnerText;

}

3.Web Service的GetUpdateData方法。

[WebMethod(Description="在线更新软件")]

[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.在客户端进行的工作。

首先引用此Web Service,例如命名为: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文件里指定的路径和文件名来生成指定的文件,在这之前先前已经存在的文件删除。更新完毕后,则重新启动主应用程序。这样更新就完成了。

四、总结:

从这个实例看来,Web Service的工作是很简单的,也是很容易实现的。好好的使用Web Service能够为我们的程序带来很多新的,强的功能。总而言之,.Net是易用的,强大的语言。

在WinForm中使用Web Service来实现软件自动升级的更多相关文章

  1. 在WinForm中使用Web Services 来实现 软件自动升级( Auto Update ) (C#)

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

  2. 在WinForm中使用Web Services 来实现 软件 自动升级( Auto Update ) (C#)

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

  3. C#之VS2010ASP.NET页面调用Web Service和winform程序调用Web Service

    一:用ASP.NET调用Web Service 打开VS2010,打开“文件-新建-网站”,选择“ASP.NET网站” 选好存储位置,语言后点击确定,进入默认页面.然后先添加Web引用,把WebSer ...

  4. 你会在C#的类库中添加web service引用吗?

    本文并不是什么高深的文章,只是VS2008应用中的一小部分,但小部分你不一定会,要不你试试: 本人对于分布式开发应用的并不多,这次正好有一个项目要应用web service,我的开发环境是vs2008 ...

  5. 微软BI 之SSIS 系列 - 在 SSIS 中使用 Web Service 以及 XML 解析

    开篇介绍 Web Service 的用途非常广几乎无处不在,像各大门户网站上的天气预报使用到的第三方 Web Service API,像手机客户端和服务器端的交互等都可以通过事先设计好的 Web Se ...

  6. Visual Studio 2013中引入Web Service的简单方法visual studio 引用 wsdl

    http://blog.csdn.net/wangzhongbo_24/article/details/49954191 Web Service有三种表示方式 三种方式分别为WSDL.Endpoint ...

  7. VS2010下创建WEBSERVICE,第二天 ----你会在C#的类库中添加web service引用吗?

    本文并不是什么高深的文章,只是VS2008应用中的一小部分,但小部分你不一定会,要不你试试: 本人对于分布式开发应用的并不多,这次正好有一个项目要应用web service,我的开发环境是vs2008 ...

  8. 在C#中实现软件自动升级

    在C#中实现软件自动升级 winform程序相对web程序而言,功能更强大,编程更方便,但软件更新却相当麻烦,要到客户端一台一台地升级,本文结合实际情况,通过软件实现自动升级,弥补了这一缺陷,有较好的 ...

  9. [转]WinForm如何调用Web Service

    1.建立项目WebService和WinForm项目,这里起名为WinFormInvokeWebService,如图所示, 2.Service1.asmx代码为:(这部分其实和上篇的代码是一样的) u ...

随机推荐

  1. UVa 11889 (GCD) Benefit

    好吧,被大白书上的入门题给卡了.=_=|| 已知LCM(A, B) = C,已知A和C,求最小的B 一开始我想当然地以为B = C / A,后来发现这时候的B不一定满足gcd(A, B) = 1 A要 ...

  2. bzoj1863: [Zjoi2006]trouble 皇帝的烦恼

    白书原题.l边界又设错啦.一般都是错这里吧.注意为什么这里不能是l=0.(只是为了判断第一个和最后一个 #include<cstdio> #include<cstring> # ...

  3. iphone 如何清空UIWebView的缓存

      iphonecachingapplicationcookiescacheperformance I actually think it may retain cached information ...

  4. git - svn 平滑到 git

    1. 建立自己的git仓库,需要是空git仓库 2. checkout 你的 git仓库 3. svn忽略.git文件,忽略.git  .gitignore 4. 把 .git文件拷到你的 svn仓库 ...

  5. Mac 配置jdk

    1.打开终端,开始操作 cd ~touch.bash_profile vi .bash_profile 2.在此文本中添加以下内容 export JAVA_HOME=/Library/Java/Jav ...

  6. JAVA数据库处理(连接,数据查询,结果集返回)

    package john import java.io.IOException; import java.util.*; public class QueryDataRow { public Hash ...

  7. [Everyday Mathematics]20150202

    设 $f:\bbR^2\to \bbR$ 为连续函数, 且满足条件 $$\bex f(x+1,y)=f(x,y+1)=f(x,y),\quad\forall\ (x,y)\in \bbR^2. \ee ...

  8. 1050 数的计数 c语言实现

    描述 给定一个正整数,求其各位之和. 输入 输入一行,为一个正整数(最多10,000位). 输出 输出各位之和. 样例输入 17 样例输出 8 解析:这题主要是大数计算的问题,因为10000位的数无法 ...

  9. SQL删除数据库里所有表的外键,同时删除所有用户表

    SQL删除数据库里所有表的外键,同时删除所有用户表 删除所有的用户表的外键,直接将下面的代码拷贝到数据库里执行即可: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...

  10. IOS AsyncSocket

    导入AsyncSocket.h  AsyncSocket.m   AsyncUdpSocket.h   AsyncUdpSocket.m   以及  CFNetWork.framework async ...