升级的原理有好几个,首先无非是将现有版本与最新版本作比较,发现最新的则提示用户是否升级。当然也有人用其它属性比较的,例如:文件大小,或者更新日期。而实现的方法呢?

在.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="" 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=;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),,(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=;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),
,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. 基于Quick-cocos2d-x的资源更新方案 二
  2. net2.0对于递归变量的处理方式不同引发的递归问题
  3. Caliburn.Micro(CM) 穿过 Popup 绑定方法
  4. Hamming Codes
  5. scala函数定义的四种方式
  6. 【Andorid开发框架学习】之Mina开发之服务器开发
  7. StroyBoard中UICollectionView中添加Header和footer
  8. poj3294 --Life Forms
  9. 【转】CTS tests 4.2_r4
  10. TreeSet()详解
  11. ##7.Dashboard web管理界面-- openstack pike
  12. vue较深入的知识点
  13. Spring 快速开始 Profile 和 Bean
  14. js签名
  15. 探索未知种族之osg类生物---呼吸分解之事件循环二
  16. 下载历史版本App
  17. Java 8 Lambda排序 : Comparator example
  18. FlexPaper_1.2.1.swc——Flex在线显示PDF文档(使用FlexPaper)感悟
  19. UVa 11732 strcmp()函数(左孩子右兄弟表示法)
  20. Question: Database Of Tumor Suppressors And/Or Oncogenes

热门文章

  1. 模块(Modules)
  2. HDU2222 Keywords Search ac自动机第一题
  3. onerror=&quot;javascript:this.src=&#39;images/defaultUpload.png&#39;;&quot;【容易导致死循环报错】
  4. 关于解决Springboot跨域请求的方法
  5. 记录一些WPF常用样式方便以后复用(转)
  6. grpc xservice 使用
  7. arangodb 安装简单试用
  8. istio 安装试用
  9. visibility:hidden和display:none的区别
  10. mysql的三种安装方式(详细)