winform自动升级程序示例源码下载 

客户端思路:

1、新建一个配置文件Update.ini,用来存放软件的客户端版本:

[update] version=2011-09-09 15:26

2、新建一个单独的客户端升级程序Update.exe:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Net;

using System.Threading;

using System.Diagnostics;

using System.IO;

namespace Update

{

publicpartialclassFrmUpdate : Form

{

//关闭进度条的委托

publicdelegatevoidCloseProgressDelegate();

//声明关闭进度条事件

publiceventCloseProgressDelegate CloseProgress;

UpdateService.Service service = null;//webservice服务

WebClient wc = null;

string url;//获取下载地址

string[] zips;//获取升级压缩包

int zipsIndex = 0;//当前正在下载的zips下标

long preBytes = 0;//上一次下载流量

long currBytes = 0;//当前下载流量

public FrmUpdate()

{

InitializeComponent();

service = new UpdateService.Service();//webservice服务

url = service.GetUrl();//获取下载地址

zips = service.GetZips();//获取升级压缩包

}

privatevoid FrmUpdate_Load(object sender, EventArgs e)

{

try

{

//用子线程工作

Thread t = newThread(newThreadStart(DownLoad));

t.IsBackground = true;//设为后台线程

t.Start();

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

}

/// <summary>

/// 下载更新

/// </summary>

privatevoid DownLoad()

{

try

{

CloseProgress += newCloseProgressDelegate(FrmUpdate_CloseProgress);

if (zips.Length > 0)

{

wc = newWebClient();

wc.DownloadProgressChanged += newDownloadProgressChangedEventHandler(wc_DownloadProgressChanged);

wc.DownloadFileCompleted += newAsyncCompletedEventHandler(wc_DownloadFileCompleted);

wc.DownloadFileAsync(newUri(url + zips[zipsIndex]), zips[zipsIndex]);

}

else

{

FrmUpdate_CloseProgress();//调用关闭进度条事件

}

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

}

/// <summary>

/// 下载完成后触发

/// </summary>

void wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)

{

zipsIndex++;

if (zipsIndex < zips.Length)

{

//继续下载下一个压缩包

wc = newWebClient();

wc.DownloadProgressChanged += newDownloadProgressChangedEventHandler(wc_DownloadProgressChanged);

wc.DownloadFileCompleted += newAsyncCompletedEventHandler(wc_DownloadFileCompleted);

wc.DownloadFileAsync(newUri(url + zips[zipsIndex]), zips[zipsIndex]);

}

else

{

//解压

int maximum = ZipClass.GetMaximum(zips);

foreach (string zip in zips)

{

ZipClass.UnZip(Application.StartupPath + @"\" + zip, "", maximum, FrmUpdate_SetProgress);

File.Delete(Application.StartupPath + @"\" + zip);

}

FrmUpdate_CloseProgress();//调用关闭进度条事件

}

}

/// <summary>

/// 下载时触发

/// </summary>

void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)

{

if (this.InvokeRequired)

{

this.Invoke(newDownloadProgressChangedEventHandler(wc_DownloadProgressChanged), newobject[] { sender, e });

}

else

{

label1.Text = "正在下载自解压包" + zips[zipsIndex] + "(" + (zipsIndex + 1).ToString() + "/" + zips.Length + ")";

progressBar1.Maximum = 100;

progressBar1.Value = e.ProgressPercentage;

currBytes = e.BytesReceived;//当前下载流量

}

}

/// <summary>

/// 解压时进度条事件

/// </summary>

/// <param name="maximum">进度条最大值</param>

privatevoid FrmUpdate_SetProgress(int maximum, string msg)

{

if (this.InvokeRequired)

{

this.Invoke(new ZipClass.SetProgressDelegate(FrmUpdate_SetProgress), newobject[] { maximum, msg });

}

else

{

if (zipsIndex == zips.Length)

{

//刚压缩完

progressBar1.Value = 0;

zipsIndex++;

}

label1.Text = "正在解压" + msg + "(" + (progressBar1.Value + 1).ToString() + "/" + maximum + ")";

progressBar1.Maximum = maximum;

progressBar1.Value++;

}

}

/// <summary>

/// 实现关闭进度条事件

/// </summary>

privatevoid FrmUpdate_CloseProgress()

{

if (this.InvokeRequired)

{

this.Invoke(newCloseProgressDelegate(FrmUpdate_CloseProgress), null);

}

else

{

if (wc != null)

{

wc.Dispose();

}

if (zips.Length > 0)

{

MessageBox.Show("升级成功!");

}

else

{

MessageBox.Show("未找到升级包!");

}

IniClass ini = new IniClass(Application.StartupPath + @"\Update.ini");

string serviceVersion = service.GetVersion();//服务端版本

ini.IniWriteValue("update", "version", serviceVersion);//更新成功后将版本写入配置文件

Application.Exit();//退出升级程序

Process.Start("Main.exe");//打开主程序Main.exe

}

}

//1秒计算一次速度

privatevoid timer1_Tick(object sender, EventArgs e)

{

this.Text = ((currBytes - preBytes) / 1024).ToString() + "kb/s";//速度

preBytes = currBytes;//上一次下载流量

}

}

}

解压类ZipClass.cs:引用 ICSharpCode.SharpZipLib.dll

using System;

using System.Collections.Generic;

using System.Text;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Windows.Forms;

using System.IO;

using ICSharpCode.SharpZipLib;

using ICSharpCode.SharpZipLib.Checksums;

using ICSharpCode.SharpZipLib.Zip;

namespace Update

{

publicclassZipClass

{

//设置进度条的委托

publicdelegatevoidSetProgressDelegate(int maximum, string msg);

#region 解压

/// <summary>

/// 功能:解压zip格式的文件。

/// </summary>

/// <param name="zipFilePath">压缩文件路径,全路径格式</param>

/// <param name="unZipDir">解压文件存放路径,全路径格式,为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹</param>

/// <param name="err">出错信息</param>

/// <returns>解压是否成功</returns>

publicstaticbool UnZip(string zipFilePath, string unZipDir, int maximum, SetProgressDelegate setProgressDelegate)

{

if (zipFilePath == string.Empty)

{

thrownew System.IO.FileNotFoundException("压缩文件不不能为空!");

}

if (!File.Exists(zipFilePath))

{

thrownew System.IO.FileNotFoundException("压缩文件: " + zipFilePath + " 不存在!");

}

//解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹

if (unZipDir == string.Empty)

unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), "");

if (!unZipDir.EndsWith("//"))

unZipDir += "//";

if (!Directory.Exists(unZipDir))

Directory.CreateDirectory(unZipDir);

try

{

using (ZipInputStream s = newZipInputStream(File.OpenRead(zipFilePath)))

{

ZipEntry theEntry;

while ((theEntry = s.GetNextEntry()) != null)

{

string directoryName = Path.GetDirectoryName(theEntry.Name);

string fileName = Path.GetFileName(theEntry.Name);

if (directoryName.Length > 0)

{

Directory.CreateDirectory(unZipDir + directoryName);

}

if (!directoryName.EndsWith("//"))

directoryName += "//";

if (fileName != String.Empty)

{

using (FileStream streamWriter = File.Create(unZipDir + theEntry.Name))

{

int size = 2048;

byte[] data = newbyte[2048];

while (true)

{

size = s.Read(data, 0, data.Length);

if (size > 0)

{

streamWriter.Write(data, 0, size);

}

else

{

setProgressDelegate(maximum, theEntry.Name);

break;

}

}

}

}

}//while

}

}

catch (Exception ex)

{

thrownewException(ex.Message);

}

returntrue;

}//解压结束

#endregion

publicstaticint GetMaximum(string[] zips)

{

int maximum = 0;

ZipInputStream s = null;

ZipEntry theEntry = null;

foreach (string zip in zips)

{

s = newZipInputStream(File.OpenRead(Application.StartupPath + @"\" + zip));

while ((theEntry = s.GetNextEntry()) != null)

{

if (Path.GetFileName(theEntry.Name) != "")

{

maximum++;

}

}

}

if (s != null)

{

s.Close();

}

return maximum;

}

}

}

3、将update.ini和Update.exe复制到主程序目录,主程序登录时判断客户端版本是否与服务端版本相同,如不同,则做升级:

IniClass ini = newIniClass(Application.StartupPath + @"\Update.ini");

UpdateService.Service service = new UpdateService.Service();

string clientVersion = ini.IniReadValue("update", "version");//客户端版本

string serviceVersion = service.GetVersion();//服务端版本

if (clientVersion != serviceVersion)

{

DialogResult dialogResult = MessageBox.Show("有新版本,是否更新?", "升级", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);

if (dialogResult == DialogResult.OK)

{

Application.Exit();

Process.Start("Update.exe");

}

}

else

{

MessageBox.Show("已更新至最高版本!");

}

服务端思路:

1、新建一个Web Service,在Web.Config中配置:

<connectionStrings>

<!--版本号-->

<add name="version" connectionString="2011-09-20 15:17"/>

<!--下载地址-->

<add name="url" connectionString ="http://localhost:8546/WebSite/"/>

<!--下载目录,最好为一级目录免得麻烦-->

<add name="directory" connectionString ="UpdateFile"/>

</connectionStrings>

2、在Service.cs写入以下代码:

using System;

using System.Web;

using System.Web.Services;

using System.Web.Services.Protocols;

using System.IO;

using System.Configuration;

[WebService(Namespace = "http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

publicclassService : System.Web.Services.WebService

{

public Service()

{

//如果使用设计的组件,请取消注释以下行

//InitializeComponent();

}

/// <summary>

/// 获取版本号

/// </summary>

/// <returns>更新版本号</returns>

[WebMethod]

publicstring GetVersion()

{

returnConfigurationManager.ConnectionStrings["version"].ConnectionString;

}

/// <summary>

/// 获取下载地址

/// </summary>

/// <returns>下载地址</returns>

[WebMethod]

publicstring GetUrl()

{

returnConfigurationManager.ConnectionStrings["url"].ConnectionString + ConfigurationManager.ConnectionStrings["directory"].ConnectionString + "/";

}

/// <summary>

/// 获取下载zip压缩包

/// </summary>

/// <returns>下载zip压缩包</returns>

[WebMethod]

publicstring[] GetZips()

{

string folder = HttpRuntime.AppDomainAppPath + ConfigurationManager.ConnectionStrings["directory"].ConnectionString;

string[] zips = Directory.GetFileSystemEntries(folder);

for (int i = 0; i < zips.Length; i++)

{

zips[i] = Path.GetFileName(zips[i]);

}

return zips;

}

}

c# winform 自动升级的更多相关文章

  1. 分享一个客户端程序(winform)自动升级程序,思路+说明+源码

    做winform的程序,不管用没用过自动更新,至少都想过自动更新是怎么实现的. 我这里共享一个自动更新的一套版本,给还没下手开始写的人一些帮助,也希望有大神来到,给指点优化意见. 本初我是通过sock ...

  2. winform 自动升级

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

  3. C/S WinForm自动升级

    这二天刚好完成一个C/S 自动升级的功能 代码分享一下 /// <summary>    /// 版本检测    /// </summary>    public class ...

  4. winform自动升级方案

    未涉及过winform升级,研究一阵,大致出来个不成熟的方案. 我的解决方案(判断升级,升级程序下载安装包的压缩包,解压,自动安装,重新启动程序). 1.首先根据服务器中软件版本号和本地软件版本号是否 ...

  5. 黄聪:C#Winform程序如何发布并自动升级(图解)

    有不少朋友问到C#Winform程序怎么样配置升级,怎么样打包,怎么样发布的,在这里我解释一下打包和发布关于打包的大家可以看我的文章C# winform程序怎么打包成安装项目(图解)其实打包是打包,发 ...

  6. 【转】C#Winform程序如何发布并自动升级(图解)

    有不少朋友问到C#Winform程序怎么样配置升级,怎么样打包,怎么样发布的,在这里我解释一下打包和发布关于打包的大家可以看我的文章C# winform程序怎么打包成安装项目(图解)其实打包是打包,发 ...

  7. C#Winform程序如何发布并自动升级(图解)

    C#Winform程序如何发布并自动升级(图解)     有不少朋友问到C#Winform程序怎么样配置升级,怎么样打包,怎么样发布的,在这里我解释一下打包和发布 关于打包的大家可以看我的文章C# w ...

  8. SNF开发平台WinForm之八-自动升级程序部署使用说明-SNF快速开发平台3.3-Spring.Net.Framework

    9.1运行效果: 9.2开发实现: 1.首先配置服务器端,把“SNFAutoUpdate2.0\服务器端部署“目录按网站程序进行发布到IIS服务器上. 2.粘贴语句,生成程序 需要调用的应用程序的Lo ...

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

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

随机推荐

  1. EF访问数据库报“ExecuteReader 要求已打开且可用的 Connection。连接的当前状态为已关闭。”错误

    我发生这个问题的原因是因为我用EF访问数据库时用的用到了两用方式,如下图 第一种方式访问时不会出现此错误,出现错误的是第二种方式,下图是dal层代码 其中红框中的代码是出现错误之后改正的代码,也就是说 ...

  2. 深入理解Java线程池:ThreadPoolExecutor

    线程池介绍 在web开发中,服务器需要接受并处理请求,所以会为一个请求来分配一个线程来进行处理.如果每次请求都新创建一个线程的话实现起来非常简便,但是存在一个问题: 如果并发的请求数量非常多,但每个线 ...

  3. EF数据库优先模式(一)

    C#中EF模式,讲述个人在做项目时用到的一些思路以及方法 EF数据模型有三种方式,database优先,model优先,Code优先,个人在做项目时用到的是database优先,以后再说其他的方式 d ...

  4. IE8中jQuery.load()加载页面不显示的原因

    一.jQuery.load() jQuery.load(url,[data],[callback])通过Ajax异步请求加载服务器中的数据,并把数据放到指定元素中. url :请求服务器的地址 dat ...

  5. 自定义高级版python线程池

    基于简单版创建类对象过多,现自定义高级版python线程池,代码如下 #高级线程池 import queue import threading import time StopEvent = obje ...

  6. WEB服务器、应用程序服务器、HTTP服务器的区别

    WEB服务器.应用程序服务器.HTTP服务器的区别 Web服务器: 基本功能就是提供Web信息浏览服务.它只需支持HTTP协议.HTML文档格式及URL.与客户端的网络浏览器配合.因为Web服务器主要 ...

  7. AJAX 简单归纳 -- 前端知识

    什么是 AJAX ? AJAX = 异步 JavaScript 和 XML. AJAX 是一种用于创建快速动态网页的技术. 通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新.这意味 ...

  8. 【代码笔记】iOS-键盘自适应弹出

    一,效果图. 二,工程图. 三,代码. ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UIVie ...

  9. 一台电脑配置多个tomcat过程

    方法1:https://jingyan.baidu.com/article/76a7e409edbb4dfc3b6e1516.html 方法2:https://www.cnblogs.com/yiyi ...

  10. CSS布局模型学习(Float、Position、Flexbox)

    一.Floatfloat 属性定义元素在哪个方向浮动.以往这个属性总应用于图像,使文本围绕在图像周围,不过在 CSS 中,任何元素都可以浮动.浮动元素会生成一个块级框,而不论它本身是何种元素. 清除浮 ...