引言

2015年第一篇,Winform在线更新,算是重操旧业吧,09年刚到北京时一直做硬件联动编程,所以大多数时间都在搞Winform, 但是从来没做过在线更新这个功能,前几天参与部门另一个项目,上来就需要加一个在线更新。 该自动更新组件核心是圣殿骑士开发的,另外参考逆水寒龙使用情况,当我拿到这个组件源代码时并不知道如何去用,花了大半天时间也没调试通过,所以把我遇到的问题和解决方法记录下来,方便后续其他Coder使用时查看使用方法。

在线更新思路

1、请求远程站点Version.xml 或者请求Web服务器都可以,最主要是对比服务器端配置文件版本号和本地配置文件版本号。

2、从远程站点下载文件。

3、覆盖本地可执行文件及用到的Dll,当然覆盖之前需要关掉本地exe进程或者windows服务进程。

4、文件覆盖后修改本地配置文件AutoUpdater.config.

5、覆盖后重新启动可执行程序或者windows服务。

该AutoUpdater缺点(使用过程中已完善)

1、没看到圣殿骑士如何使用该组件,参考逆水寒龙Demo时,找到了主程序关闭的地方。

2、在下载文件时没有考虑到远程服务器新增文件时处理本地AutoUpdater.config,也就是说更新程序处理时是基于本地配置文件config.UpdateFileList。

3、没有针对windows服务处理进程(已完善windows服务处理类)。

使用时遇到的问题

1、不知道如何使用该组件?

使用该组件时项目应该具备4部分:Web站点用于接受请求及下载需更新文件、AutoUpdater更新组件、MainApplication(也就是你的主程序)、UpdateApplication(更新程序入口,通过主程序启动,在这个程序内部调用AutoUpdater组件)。

2、 不知道合适去关闭主程序和windows服务

后在看逆水寒龙的Demo中看到他是在文件下载前,当你点击确认下载时关闭进程。

if (bDownload)
{
//关闭windows service
ServiceProcess serviceProcess = new ServiceProcess();
serviceProcess.StopService(); //关掉主程序
OperProcess oper=new OperProcess();
oper.InitUpdateEnvironment(); DownloadConfirm dc = new DownloadConfirm(downloadList);
if (this.OnShow != null)
this.OnShow(); if (DialogResult.OK == dc.ShowDialog())
{
StartDownload(downloadList);
}
}

3、文件下载后临时目录和源程序主目录口径不一致

这个根据自己需要调整文件Copy、Move代码。我们的程序可执行文件和主要Dll都是放在一个单独的文件里而不是默认的Bin\Debug下。所以我的做法就是把下载文件从TempFolder移动到可执行源文件根目录下。具体代码在DownloadProgress窗体的ProcDownload方法中处理。

if (!string.IsNullOrEmpty(tempFolderPath))
{
oldPath = Path.Combine(CommonUnitity.SystemBinUrl, file.FileName);
newPath = Path.Combine(CommonUnitity.SystemBinUrl + ConstFile.TEMPFOLDERNAME + tempUrlPath, file.FileName);
}

4、 文件下载移动复制后合适启动应用程序

在更新程序里添加finally代码,启动windows服务、启动主程序、关闭更新程序(UpdateApplication)。

void InitAndCheck()
{
bool bHasError = false;
IAutoUpdater autoUpdater = new AutoUpdater(); try
{
autoUpdater.Update();
}
catch (WebException exp)
{
MessageBox.Show("Can not find the specified resource");
bHasError = true;
}
catch (XmlException exp)
{
bHasError = true;
MessageBox.Show("Download the upgrade file error");
}
catch (NotSupportedException exp)
{
bHasError = true;
MessageBox.Show("Upgrade address configuration error");
}
catch (ArgumentException exp)
{
bHasError = true;
MessageBox.Show("Download the upgrade file error");
}
catch (Exception exp)
{
bHasError = true;
MessageBox.Show("An error occurred during the upgrade process");
}
finally
{
if (bHasError == true)
{
try
{
autoUpdater.RollBack();
}
catch (Exception)
{ }
}
ServiceProcess serviceProcess = new ServiceProcess();
serviceProcess.StartService(); OperProcess oper = new OperProcess();
oper.StartProcess();
}
}

5、 当Web站点出现新增文件时没有同步到本地配置文件。

看源代码是发现修改本地AutoUpdater.config是基于本地UpdateFileList,只需要把Web端新增文件信息添加到该List中,序列化本地config时就会同步。

LocalFile tempFile = null;
foreach (RemoteFile file in listRemotFile.Values)
{
downloadList.Add(new DownloadFileInfo(file.Url, file.Path, file.LastVer, file.Size)); //把远程服务器新增文件加入config.UpdateFileList, 修改本地AutoUpdater.config时使用。
tempFile = new LocalFile(file.Path, file.LastVer, file.Size);
config.UpdateFileList.Add(tempFile); if (file.NeedRestart)
bNeedRestart = true;
}

6、添加windows服务处理类

public class ServiceProcess
{
#region 判断window服务是否启动
/// <summary>
/// 判断某个Windows服务是否启动
/// </summary>
/// <returns></returns>
private bool IsServiceStart(string serviceName)
{
ServiceController psc = new ServiceController(serviceName);
bool bStartStatus = false;
try
{
if (!psc.Status.Equals(ServiceControllerStatus.Stopped))
{
bStartStatus = true;
} return bStartStatus;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
#endregion #region 启动服务
private bool StartService(string serviceName)
{
bool flag = true;
if (ServiceIsExisted(serviceName))
{
System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);
if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)
{
service.Start();
for (int i = 0; i < 60; i++)
{
service.Refresh();
System.Threading.Thread.Sleep(1000);
if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
{
break;
}
if (i == 59)
{
flag = false;
}
}
}
}
return flag;
}
#endregion #region 停止服务
private bool StopService(string serviceName)
{
bool flag = true;
if (ServiceIsExisted(serviceName))
{
System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);
if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
{
service.Stop();
for (int i = 0; i < 60; i++)
{
service.Refresh();
System.Threading.Thread.Sleep(1000);
if (service.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
{
break;
}
if (i == 59)
{
flag = false;
}
}
}
}
return flag;
}
#endregion #region 判断window服务是否存在
private bool ServiceIsExisted(string serviceName)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController s in services)
{
if (s.ServiceName == serviceName)
{
return true;
}
}
return false;
}
#endregion #region 安装服务
private void InstallService(IDictionary stateSaver, string filepath)
{
try
{
ServiceController service = new ServiceController("ServiceName");
if (!ServiceIsExisted("ServiceName"))
{
//Install Service
AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();
myAssemblyInstaller.UseNewContext = true;
myAssemblyInstaller.Path = filepath;
myAssemblyInstaller.Install(stateSaver);
myAssemblyInstaller.Commit(stateSaver);
myAssemblyInstaller.Dispose();
//--Start Service
service.Start();
}
else
{
if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)
{
service.Start();
}
}
}
catch (Exception ex)
{ }
}
#endregion #region 卸载windows服务
private void UnInstallService(string filepath)
{
try
{
if (ServiceIsExisted("ServiceName"))
{
//UnInstall Service
AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();
myAssemblyInstaller.UseNewContext = true;
myAssemblyInstaller.Path = filepath;
myAssemblyInstaller.Uninstall(null);
myAssemblyInstaller.Dispose();
}
}
catch (Exception ex)
{ }
}
#endregion public void StopService()
{
if (ServiceIsExisted("MyService"))
{
if (IsServiceStart("MyService"))
{
this.StopService("MyService");
}
} } public void StartService()
{ if (ServiceIsExisted("MyService"))
{
if (!IsServiceStart("MyService"))
{
this.StartService("MyService");
}
} }
}

总结

当有任何一个功能需要开发时先要弄懂其原理,并且能反复调试网上找来的源码,反复调试后能明白其中的原理才能更好的应用。初拿到这个功能时并不明白就想去集成,结果花了时间还没弄明白怎么使用。 再次感谢圣殿骑士和逆水寒龙的文章给我的启示。

Demo下载 提取码99a9

Winform在线更新的更多相关文章

  1. winform 自动升级

    自动升级系统OAUS的设计与实现(续) (附最新源码) http://www.cnblogs.com/zhuweisky/p/4209058.html Winform在线更新 http://www.c ...

  2. Vs2017发布可在线更新的Winform程序

    如题,此处引用“南秦岭”的博文<使用ClickOnce发布Windows应用程序>,对作者表示感谢! 补充说明: “发布文件夹”是指你电脑上的本地文件夹:“安装文件夹”是指你提供给用户的u ...

  3. 在WinForm中使用Web Service来实现软件自动升级

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

  4. 客户端(winform)更新

    winform更新有两种情况,一种是在线更新在线使用:直接右击项目发布出去就可以更新在线使用了.还有一种更新是不用一直连接网络的模式. 1:C#Winform程序如何发布并自动升级--------ht ...

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

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

  6. WinForm应用程序中实现自动更新功能

    WinForm应用程序中实现自动更新功能 编写人:左丘文 2015-4-20 近来在给一客户实施ECM系统,但他们使用功能并不是我们ECM制造版提供的标准功能,他们要求对系统作一些定制功能,为了避免因 ...

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

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

  8. C#[WinForm]实现自动更新

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

  9. 使用 .NET WinForm 开发所见即所得的 IDE 开发环境,实现不写代码直接生成应用程序

    直接切入正题,这是我09年到11年左右业余时间编写的项目,最初的想法很简单,做一个能拖拖拽拽就直接生成应用程序的工具,不用写代码,把能想到的业务操作全部封装起来,通过配置的方式把这些业务操作组织起来运 ...

随机推荐

  1. java获取cpu和内存

    利用jar包sigar 下载地址:http://sourceforge.net/projects/sigar/files/latest/download?source=files 需要将sigar-x ...

  2. 用C语言编写生成小学四则运算程序

    使用软件——VS 2015 使用环境——C语言 早在上周我就开始使用C#语言做,由于最后一点问题而放弃,之后用C语言开始做,很顺利,但是也碰到了一些问题,但是通过了百度文库上的一些程序的借鉴和吴阿平同 ...

  3. rails4.0 session activerecord

    Active Record Session Store A session store backed by an Active Record class. A default class is pro ...

  4. Android零散

    2016-03-13 Android零散 ListView中嵌套GridView 要实现分组列表这样的效果:点击ListView中的分组名称,即展开此分组显示其包含的项目.使用ExpandableLi ...

  5. 一步步学习javascript基础篇(2):作用域和作用域链

    作用域和作用域链 js的语法用法非常的灵活,且稍不注意就踩坑.这集来分析下作用域和作用域链.我们且从几道题目入手,您可以试着在心里猜想着答案. 问题一. if (true) { var str = & ...

  6. Scala 笔记

    环境 1. Intellij Idea 2. Scala 插件 3. http://scala-lang.org/ 教程: idea官方教程: https://www.jetbrains.com/he ...

  7. HTML和CSS经典布局2

    如下图: 需求: 1. 如图 2. 可以从body标签开始. <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xht ...

  8. angularjs 指令详解 - template, restrict, replace

    通过指令机制,angularjs 提供了一个强大的扩展系统,我们可以通过自定义指令来扩展自己的指令系统. 怎样定义自己的指令呢? 我们通过 Bootstrap UI来学习吧.这个项目使用 angula ...

  9. 让浏览器不再显示 https 页面中的 http 请求警报

    HTTPS 是 HTTP over Secure Socket Layer,以安全为目标的 HTTP 通道,所以在 HTTPS 承载的页面上不允许出现 http 请求,一旦出现就是提示或报错: Mix ...

  10. 使用 WPF+ ASP.NET MVC 开发 在线客服系统 (一)

    近段时间利用业余时间开发了一套在线客服系统,期间遇到过大大小小不少问题,好在都一一解决,最终效果也还可以,打算写一个系列的文章把开发过程详细的记录下来. 希望能够和更多的开发人员互相交流学习,也希望有 ...