上一篇文章解决了实现可复用的自动升级系统的思路,这篇文章将给出UpdateActionSystem.exe的参考实现及相关的牵涉主程序的代码。
UpdateActionSystem.exe的主窗体UpdatingForm的主要成员如下:
private UpdateConfigParser updateParser = null ;//用于解析版本配置文件UpdateConfig.xml
private DealSoftwareVersion dealVersion = null ;//用于访问数据库的SoftwareVersion表
private string curApppath = null ;//当前路径
//构造函数中初始化各成员
#region ctor
public UpdatingForm()
{
InitializeComponent();
this.curApppath = System.IO.Directory.GetParent(Application.ExecutablePath).ToString();
this.progressControl2.SetIDealEvent(this) ;
this.updateParser = new UpdateConfigParser(this.curApppath + "//UpdateConfig.xml") ;
string connStr =...... ;
this.dealVersion = new DealSoftwareVersion(connStr) ;
}
#endregion
//下载新版本文件的线程:
#region DownloadThread
private void DownloadThread()
{
int succeedCount = 0 ;
int failCount = 0 ;
SoftwareVersion[] versions = null ;
try
{
versions = (SoftwareVersion[])this.dealVersion.GetObjects("") ;
if((versions == null) || (versions.Length == 0))
{
MessageBox.Show("没有任何文件需要升级!") ;
this.Close() ;
}
}
catch
{
MessageBox.Show("无法与数据库服务器建立连接,可能是服务器已关闭!升级失败!") ;
this.Close() ;
}
for(int i=0 ;i<versions.Length ;i++)
{
if((versions[i].SoftwareType != this.updateParser.SoftwareType) && (versions[i].SoftwareType != this.updateParser.CommonSoftwareTypeName))
{
continue ;
}
string softName = versions[i].SoftwareName ;
decimal newVer = versions[i].Version ;
string url = versions[i].URL ;
string filePath = this.curApppath + "//" + softName ;
bool isExit = this.updateParser.IsSoftwareExit(softName) ;
if(isExit)
{
decimal oldVer = this.updateParser.GetSoftwareVersion(softName) ;
if(oldVer < newVer) //覆盖旧文件
{
this.SetTitle(softName ,true) ;
bool succeed = this.DownLoadOneFile(url ,filePath ,this.progressControl2) ;
if(succeed)
{
this.updateParser.SetNewVersion(softName ,newVer) ;
++ succeedCount ;
this.DisplayMsg(string.Format("成功升级{0}文件!" ,softName)) ;
}
else
{
++ failCount ;
this.DisplayMsg(string.Format("升级{0}文件失败!" ,softName)) ;
}
}
}
else //下载新文件
{
this.SetTitle(softName ,false) ;
this.updateParser.AddNewSoftware(softName ,newVer) ;
bool succeed = this.DownLoadOneFile(url ,filePath ,this.progressControl2) ;
if(succeed)
{
++ succeedCount ;
this.DisplayMsg(string.Format("成功下载{0}文件!" ,softName)) ;
}
else
{
++ failCount ;
this.updateParser.SetNewVersion(softName ,0) ;
this.DisplayMsg(string.Format("下载{0}文件失败!" ,softName)) ;
}
}
}
string msg = null ;
if(succeedCount+failCount == 0)
{
msg = "没有任何文件需要更新!" ;
}
else if((succeedCount > 0) && (failCount == 0))
{
msg = string.Format("成功更新{0}个文件!",succeedCount) ;
}
else
{
msg = string.Format("总共需要更新{0}个文件,成功更新{1}个文件!有{2}个文件更新失败!" , succeedCount+failCount ,succeedCount ,failCount) ;
}
MessageBox.Show(msg) ;
this.Close() ;
}
#endregion
//下载某个文件的方法
#region DownLoadOneFile
private bool DownLoadOneFile(string url ,string filePath ,ProgressControl proControl)
{
FileStream fstream = new FileStream(filePath ,FileMode.Create ,FileAccess.Write);
WebRequest wRequest = WebRequest.Create(url);
try
{
WebResponse wResponse = wRequest.GetResponse();
int contentLength =(int)wResponse.ContentLength;
byte[] buffer = new byte[1024];
int read_count = 0 ;
int total_read_count = 0 ;
bool complete = false;
proControl.SetParas(0 ,contentLength ,buffer.Length) ;
while (!complete )
{
read_count = wResponse.GetResponseStream().Read(buffer,0,buffer.Length);
if(read_count > 0)
{
fstream.Write(buffer ,0 ,read_count) ;
total_read_count += read_count ;
if(total_read_count <= contentLength)
proControl.step_forward() ;
}
else
{
complete = true ;
}
}
fstream.Flush() ;
return true ;
}
catch(Exception ee)
{
ee = ee ;
return false ;
}
finally
{
fstream.Close() ;
wRequest = null;
}
}
#endregion
//在窗体加载时,启动下载线程
#region Form1_Load
private void Form1_Load(object sender, System.EventArgs e)
{
System.Threading.Thread.Sleep(1000) ;
ThreadDelegate thread = new ThreadDelegate(this.DownloadThread) ;
thread.BeginInvoke(null ,false) ; //异步执行
}
#endregion
UpdateActionSystem.exe的主要实现代码就是这些,下面给出主应用程序中的相关代码:
在登录窗体的构造函数中:
//检查自动更新
bool needUpdate = this.NeedToUpdate() ;
this.linkLabel_update.Visible = needUpdate ;
this.button_logon.Enabled = (! needUpdate) ;
//点击linkLabel_update将启动UpdateActionSystem.exe
private void linkLabel_update_LinkClicked(object sender, System.Windows.Forms.LinkLabel_updateClickedEventArgs e)
{
Process downprocess = new Process();
string apppath = System.IO.Directory.GetParent(Application.ExecutablePath).ToString();
downprocess.StartInfo.FileName = string.Format("{0}//{1}" , apppath ,UpdateActionSystem.exe) ;
downprocess.Start();
this.DialogResult = DialogResult.Cancel;
return;
}
好了,主要的实现代码都给出来了,如果你想获取UpdateActionSystem.exe的完整源程序,可以email向我索取!sky.zhuwei@163.com
分享到:
相关推荐
长期以来,广大程序员为到底是使用Client/Server,还是使用Browser/Server结构争论不休,在这些争论当中,C /S结构的程序可维护性差,布置困难,升级不方便...下面我为大家提供了一套可复用 的用C#编写的自动升级系统。
这是我的Blog文章《在C#中实现Socket端口复用》中的例程源代码。因为我很懒,所以代码里面并没有写注释,大家只能自己去看看了!代码是用VS2005写的。
时分多路复用系统的仿真实现.docx时分多路复用系统的仿真实现.docx时分多路复用系统的仿真实现.docx时分多路复用系统的仿真实现.docx时分多路复用系统的仿真实现.docx时分多路复用系统的仿真实现.docx时分多路复用...
c# 导入 委托 进度条 可复用
一个可以设置进度条个数的窗口,基本可以直接用于项目之中。觉得不错就点个赞
通信原理课程设计频分复用dsb调制模拟系统,主要用于多路信号复用技术
时分复用2FSK通信系统系统,使用systemview进行仿真,效果不错
时分多路复用系统的仿真实现.pdf时分多路复用系统的仿真实现.pdf时分多路复用系统的仿真实现.pdf时分多路复用系统的仿真实现.pdf时分多路复用系统的仿真实现.pdf时分多路复用系统的仿真实现.pdf时分多路复用系统的...
实验七时分复用数字基带通信系统.doc
有没有想过把22端口 3389端口 80端口一次性绑在一个端口上 在对外端口非常紧缺的时候,这种做法非常重要 haproxy是一个传统的负载均衡...实现对外端口的复用。 本附件里面的例子就可以实现这三个端口的复用,好用加分。
使用systemview实现PCM时分复用课程设计包含一个通信系统,三个子系统的仿真,另外有课程设计报告成品16页,有什么问题可以联系
设计模式 - 可复用面向对象软件的基础 04
3、socket功能类库模块(其他项目也可以引用),socket类库模块功能齐全,只需调用其中方法即可,复用性较强,代码注释详细,实现了心跳,解决了粘包问题,异步发送接收数据,等等,bin目录下右运行日志方便查找程序...
基于MATLAB频分复用系统的研究与仿真设计
SDH中E3复用_解复用系统的FPGA实现.pdf
使用matlab实现偏振解复用的仿真,毕业设计的一部分,可以为你的毕设、项目提供思路。 *系统参数设定* *光纤信道PMD参数设定* *插入时域上的训练序列用于解偏振* *从频域变到时域―IFFT变换* *去循环前缀* *从时域变...
MPEG_2传输流复用解复用的研究及实现论文,写的那叫一个相当好!
本书结合设计实例从面向对象的设计中精选23个设计模式,总结了面向对象设计中最有价值的经验,并且用简洁可复用的形式表达出来。
大三通信系统课程设计_语音信号线性调制频分复用系统设计,含systemView仿真原图和课程设计报告