您的位置:首页 > 编程语言 > C#

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

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

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

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

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

PublicSubCheckUpdate()
OnErrorResumeNext
DimbAsBoolean
DimXmlHttpAsObject
SetXmlHttp=CreateObject("Microsoft.XMLHttp")
XmlHttp.Open"GET","Http://mu.5inet.net/MuAdmin/update.xml",False
XmlHttp.Send

DimvsAsString
vs=XmlHttp.responseText
IfErr.Number>0Then
ExitSub
EndIf

DimXmlAsObject
SetXml=CreateObject("Microsoft.XmlDom")
Xml.LoadXmlvs
DimVersionAsString
DimdownAddrAsString
DimFSizeAsLong
DimfInfoAsString
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
SetXml=Nothing
SetXmlHttp=Nothing

DimMajorAsLong
DimMinorAsLong
DimRevisionAsLong
DimC()AsString
C=Split(Version,".")
Major=CLng(C(0))
Minor=CLng(C(1))
Revision=CLng(C(2))

IfMajor>App.MajorThen
b=True
ElseIfMinor>App.MinorThen
b=True
ElseIfRevision>App.RevisionThen
b=True
Else
b=False
EndIf
If(b)Then
DimresultAsVbMsgBoxResult
result=MsgBox("发现程序新版本。当前版本为:"&App.Major&"."&App.Minor&"."&App.Revision&",目前最新版本为:"&Version&",是否进行更新?",vbQuestionOrvbYesNo,"自动更新")
Ifresult=vbYesThen
DimfrmAsNewUpdate
frm.DownloadAddress=downAddr
frm.size=FSize
frm.InfoPage=fInfo
frm.Version=Version
frm.ShowvbModal
EndIf
EndIf
EndSub

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

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

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

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


[WebMethod(Description="取得更新版本")]
publicstringGetVer()
{
XmlDocumentdoc=newXmlDocument();
doc.Load(Server.MapPath("update.xml"));
XmlElementroot=doc.DocumentElement;
returnroot.SelectSingleNode("version").InnerText;
}
     3、WebServices的GetUpdateData方法。
[WebMethod(Description="在线更新软件")]
[SoapHeader("sHeader")]
publicSystem.Xml.XmlDocumentGetUpdateData()
{
//验证用户是否登陆
if(sHeader==null)
returnnull;
if(!DataProvider.GetInstance.CheckLogin(sHeader.Username,sHeader.Password))
returnnull;
//取得更新的xml模板内容
XmlDocumentdoc=newXmlDocument();
doc.Load(Server.MapPath("update.xml"));
XmlElementroot=doc.DocumentElement;
//看看有几个文件需要更新
XmlNodeupdateNode=root.SelectSingleNode("filelist");
stringpath=updateNode.Attributes["sourcepath"].Value;
intcount=int.Parse(updateNode.Attributes["count"].Value);
//将xml中的value用实际内容替换
for(inti=0;i<count;i++)
{
XmlNodeitemNode=updateNode.ChildNodes[i];
stringfileName=path+itemNode.Attributes["name"].Value;
FileStreamfs=File.OpenRead(Server.MapPath(fileName));
itemNode.Attributes["size"].Value=fs.Length.ToString();
BinaryReaderbr=newBinaryReader(fs);
//这里是文件的实际内容,使用了Base64String编码
itemNode.SelectSingleNode("value").InnerText=Convert.ToBase64String(br.ReadBytes((int)fs.Length),0,(int)fs.Length);
br.Close();
fs.Close();
}
returndoc;
}

    4、在客户端进行的工作。
      首先引用此WebServices,例如命名为:WebSvs,
stringnVer=Start.GetService.GetVer(); 
if(Application.ProductVersion.CompareTo(nVer)<=0)
update();

在本代码中Start.GetService是WebSvs的一个Static实例。首先检查版本,将结果与当前版本进行比较,
如果为新版本则执行UpDate方法。

[code]		voidupdate()
{
this.statusBarPanel1.Text="正在下载...";
System.Xml.XmlDocumentdoc=((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的内容。
		privatevoidForm1_Load(objectsender,System.EventArgse)
{
System.Diagnostics.Process[]ps=System.Diagnostics.Process.GetProcesses();
foreach(System.Diagnostics.Processpinps)
{
//MessageBox.Show(p.ProcessName);
if(p.ProcessName.ToLower()=="customerapplication")
{
p.Kill();
break;
}
}
XmlDocumentdoc=newXmlDocument();
doc.Load(Application.StartupPath+@"\update.xml");
XmlElementroot=doc.DocumentElement;
XmlNodeupdateNode=root.SelectSingleNode("filelist");
stringpath=updateNode.Attributes["sourcepath"].Value;
intcount=int.Parse(updateNode.Attributes["count"].Value);
for(inti=0;i<count;i++)
{
XmlNodeitemNode=updateNode.ChildNodes[i];
stringfileName=itemNode.Attributes["name"].Value;
FileInfofi=newFileInfo(fileName);
fi.Delete();
//File.Delete(Application.StartupPath+@"\"+fileName);
this.label1.Text="正在更新:"+fileName+"("+itemNode.Attributes["size"].Value+")...";
FileStreamfs=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文件里指定的路径和文件名来生成指定的文件,在这之
前先前已经存在的文件删除。更新完毕后,则重新启动主应用程序。这样更新就完成了。
[/code]
四、总结:
  从这个实例看来,WebService的工作是很简单的,也是很容易实现的。好好的使用WebService能够为我们
的程序带来很多新的,强的功能。总而言之,.Net是易用的,强大的语言。如果大家对本代码有任何意见,欢迎
光临《开发者》论坛:http://forums.coder.cn/,希望和大家共同探讨。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐