当前位置:文档之家› C#更新升级WinForm客户端说明及代码

C#更新升级WinForm客户端说明及代码

C#开发的Windows Form 应用程序的升级说明★1.在服务器段创建一个可共享的文件夹,或者在服务器的IIS(Internet信息服务)中建立一个可供下载更新文件的目录。

如服务端中的 E:\UpGrade\★2.在服务器端(如E:\UpGrade\)中创建一个xml文件update.xml,该update.xml的内容如下:<?xml version="1.0" encoding="gb2312" ?><AutoUpdater><Url>\\192.168.6.223\UpGrade\</Url><Application><AppName>HrManager.exe</AppName><AppVersion>1.2.0.0</AppVersion></Application><Files><File Name="hrBll.dll" Vers="1.0.0.0"></File><File Name="hrBll.pdb" Vers="1.0.0.0"></File><File Name="HrManager.exe" Vers="1.0.0.0"></File><File Name="HrManager.pdb" Vers="1.0.0.0"></File><File Name="HrManager.vshost.exe" Vers="1.0.0.0"></File><File Name="HrManager.vshost.exe.manifest" Vers="1.0.0.0"></File><File Name="hrModel.dll" Vers="1.0.0.0"></File><File Name="hrModel.pdb" Vers="1.0.0.0"></File><File Name="MenuMT.xml" Vers="1.0.0.0"></File><File Name="MTMenu.xml" Vers="1.0.0.0"></File></Files></AutoUpdater>其中:AppName节点存放应用程序名AppVersion节点存在最新程序的版本号(服务器版本号)Files节点中的File指的是需要更新的文件名★3.服务器端E:\UpGrade\的内容(如下图):update.xml中主要设置应用程序名、最新程序的版本号、所有需要更新的文件名。

详情见上面的update.xml内容。

hrBll.dll、HrManager.exe、hrModel.dll等文件(注意:不包含update.xml)就是需要在客户端更新的文件。

(客户端如果不存在这些文件,就添加这些文件;客户端如果存在这些文件,就用服务端的这些文件覆盖掉)★4.在客户端的应用程序目录下,增加两个文件:Update.exe和update.xml其中客户端的update.xml的详细内容如下:<?xml version="1.0" encoding="gb2312" ?><AutoUpdater><Url>\\192.168.6.223\UpGrade\</Url><Application><AppName>HrManager.exe</AppName><AppVersion>1.1.0.0</AppVersion></Application></AutoUpdater>其中:Url节点存放服务端的下载地址(即:更新文件的下载地址)AppName节点存放应用程序名AppVersion存放本地客户端的版本号★5.升级条件:【服务端的update.xml的AppVersion的版本号】与【客户端的update.xml的AppVersion的版本号】不同,就升级。

客户端升级完成后,将【客户端的update.xml的AppVersion的版本号】修改为服务端的版本号。

★6.客户端的Update.exe文件是由WinForm应用程序生成。

来源:在Visual Studio 2005(或以上版本均可)新建项目→Windows窗体应用程序命名为Update,删除默认的Form1窗体,并添加Windows窗体FrmUpdate。

在窗体FrmUpdate中加入是哪个控件:Button:btnUpdate 、ProgressBar:progressBar1 、RichTextBox: rchtxtInfo窗体如下图:FrmUpdate的详细代码如下(按钮btnUpdate 的 click事件--btnUpdate_Click) ★源代码:using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Xml;using System.IO;using ;using System.Diagnostics;namespace Update{public partial class FrmUpdate : Form{public FrmUpdate(){InitializeComponent();progressBar1.Visible = false;//进度条控件隐藏}///<summary>///按钮更新的Click事件///</summary>///<param name="sender"></param>///<param name="e"></param>private void btnUpdate_Click(object sender, EventArgs e){string appName = GetAppName();//应用程序进程名KillAppProcess(appName);//如果应用程序正在运行,则强行关闭FolderBrowserDialog fbd = new FolderBrowserDialog();fbd.Description = "请选择程序所在的目录;fbd.SelectedPath = AppDomain.CurrentDomain.BaseDirectory;//@"E:\Hr";if (fbd.ShowDialog() == DialogResult.OK){//查看选择的目录是否含有HrManager.exeif (!File.Exists(fbd.SelectedPath + "\\" + appName)){MessageBox.Show("目录错误请选择应用程序所在的目录", "提示");return;}//获?得Ì?服¤t务?器¡Â目?录? 或¨°者? 服¤t务?器¡Â下?载?地Ì?址¡¤,//如¨?:êo\\192.168.6.223\UpGrade\string url = GetUrl();if (!Directory.Exists(url)) //查¨¦看¡ä本À?机¨²客¨ª户¡ì端?能¨¹否¤?连¢?接¨®服¤t务?器¡Â目?录?{MessageBox.Show("无法连接到服务器...", "提示");return;}//服务器的update.xml与客户端的update.xml 比较版本号是否一致?string serverVersion = GetServerVersion(url);string clientVersion = GetClientVersion(fbd.SelectedPath);rchtxtInfo.Text = "服务器版本:" + serverVersion + "\n客户端版本:" + clientVersion + "\n";Application.DoEvents();if (serverVersion == clientVersion){rchtxtInfo.Text = rchtxtInfo.Text + "已经是最新版本,不需要更新.\n";Application.DoEvents();return;}else{rchtxtInfo.Text = rchtxtInfo.Text + "不是最新版本,开始更新...\n";Application.DoEvents();}//本À?地Ì?版ã?本À?与®?服¤t务?器¡Â版ã?本À?不?一°?致? 需¨¨要°a更¨¹新?this.Cursor = Cursors.WaitCursor;progressBar1.Visible = true;//滚?动¡¥条¬?显?示º?List<string> list = GetUpdateFiles(url);for (int i = 0; i < list.Count; i++){if (DownUpdateFile(url + list[i], fbd.SelectedPath + "\\" + list[i]) == false){rchtxtInfo.Text = rchtxtInfo.Text + "更新中出现错误.";this.Cursor = Cursors.Default;return;}}rchtxtInfo.Text = rchtxtInfo.Text + "全部更新已完成.\n";//更¨¹新?本À?地Ì?客¨ª户¡ì端?下?的Ì?update.xml的Ì?版ã?本À?号?,ê?与®?服¤t务?端?版ã?本À?号?一°?致?UpdateLocalXml(fbd.SelectedPath + "\\update.xml", serverVersion);progressBar1.Visible = false;//滚?动¡¥条¬?隐°t藏?rchtxtInfo.Text = rchtxtInfo.Text + "更新成功.";this.Cursor = Cursors.Default;}}///<summary>///更¨¹新?本À?地Ì?的Ì?配?置?文?件t的Ì?版ã?本À?号?,使º1版ã?本À?号?与®?服¤t 务?器¡Â版ã?本À?一°?致?,这a样¨´下?次ä?就¨ª不?必À?更¨¹新?了¢?///</summary>///<param name="xmlFile">本À?地Ì?xml的Ì?完ª¨º全¨?路¡¤径?</param>///<param name="version">服¤t务?器¡Â版ã?本À?号?</param>private void UpdateLocalXml(string xmlFile, string version){XmlDocument doc = new XmlDocument();doc.Load(xmlFile);XmlNode rootNode = doc.DocumentElement;for (int i = 0; i < rootNode.ChildNodes.Count; i++){if (rootNode.ChildNodes[i].Name == "Application"){rootNode.ChildNodes[i].ChildNodes[1].InnerText = version;break;}}doc.Save(xmlFile);//保À¡ê存ä?文?件t才?可¨¦以°?更¨¹新?}///<summary>///如¨?果?应®|用®?程¨¬序¨°正y在¨²运?行D,ê?则¨°强?行D关?闭À?。

相关主题