.net winform软件自动更新

   关于.NET windows软件实现自动更新,本人今天写了一个DEMO,供大家参考。

     大家先看下效果图:

       

    主要涉及到两个方面

         1. 更新软件主项目和DLL文件

         2.升级包自身的更新

       

      1.   一个项目通常包括主项目和类库项目,主项目就是启动项目,以.exe结尾,类库项目主要是DLL, 简单的说更新就是将软件本地的主项目和类库项目进行更新。

可以采用将最新的软件放到一个远程服务器上,然后每次启动本地软件时候,检查如果有更新,就从服务器上下载最新的.EXE文件和DLL文件,

来替换本地的DLL文件和exe文件

     实现思路:在本地和服务器上各放一个XML文件,里面记录了软件版本号,发布日期,要更新的DLL等一些信息,如果发现本地软件的版本号和服务器上的不相等,或者

    本地软件中的类库项目的发布时间比服务上的晚,就开始下载服务器上的文件,替换掉本地的文件。

     XML格式如下

       

<?xml version="1.0" encoding="utf-8"?>
<AutoUpdater>
  <AppName>WinUpdate</AppName>
  <ReleaseURL>http://127.0.0.1/webdown/</ReleaseURL>
  <ReleaseDate>2012/3/1 10:42:34</ReleaseDate>
  <ReleaseVersion>1.0.1.99</ReleaseVersion>
  <MinVersion>1.0.1.88</MinVersion>
  <UpdateDes>
    1、	添加打印菜单
    2、	增加DLL
    3、增加关于模块
  </UpdateDes>
  <ApplicationStart>WinUpdate.exe</ApplicationStart>
  <ShortcutIcon>ico</ShortcutIcon>
  <Releases>
    <File name="AboutForm.dll" date="2012/2/21 10:07:31" size="39" />
  </Releases>
</AutoUpdater>

 

            

      public static void DownloadFile(string localFolder, string remoteFolder, string fileName, ProgressBar bar,
                                        Label lblSize)
        {
             string url = remoteFolder + "/" + fileName;
            string path = localFolder+ fileName;
            string dir = Path.GetDirectoryName(path);
            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);

            WebRequest req = WebRequest.Create(url);
            WebResponse res = req.GetResponse();
            if (res.ContentLength == 0)
                return;

            long fileLength = res.ContentLength;
            string totalSize = FormatFileSizeDescription(bar.Maximum);
            using (Stream srm = res.GetResponseStream())
            {
                var srmReader = new StreamReader(srm);
                var bufferbyte = new byte[fileLength];
                int allByte = bufferbyte.Length;
                int startByte = 0;
                while (fileLength > 0)
                {
                    int downByte = srm.Read(bufferbyte, startByte, allByte);
                    if (downByte == 0)
                    {
                        break;
                    }
                    ;
                    startByte += downByte;
                    allByte -= downByte;
                    int progress = bar.Value + downByte;
                    progress = progress > bar.Maximum ? bar.Maximum : progress;
                    bar.Value = progress;

                    lblSize.Text = string.Format("已完成{0}/{1}", FormatFileSizeDescription(progress), totalSize);
       
                }

                var fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
                fs.Write(bufferbyte, 0, bufferbyte.Length);
                srm.Close();
                srmReader.Close();
                fs.Close();
            }
        }
    }



 

   2.关于升级包自身的更新,采用如下思路,在服务器上放置一个TXT文件,里面存放着升级包的版本号,每次本地软件启动的时候,

 读取服务器上TXT文件的版本号和本地升级包的版本信息进行比较,如果不同,就从服务器上下载升级包。

 

    关于下载本项目都是使用WebClient进行完成的。

 

     自己可以采用如下方式进行测试

    首先,在你的IIS下面建立一个虚拟目录:http://127.0.0.1/webdown ,此目录用来放置要更新的文件,内容如下

      

      1.ReleaseList.xml和1.0.4.98文件夹主要是实现软件更新

          ReleaseList.xml存放了需要更新的内容。1.0.4.98文件夹存放了需要更新的类库和文件

     2.  AutoUpdate.exe,UpdaterVerson.txt这两个文件实现的升级包自身进行更新.

          AutoUpdate.exe是升级包,UpdaterVerson.txt存放的是升级包的版本号

    将以上内容部署到IIS下面

 

   

 

    本代码使用VS2008进行开发,

     DEMO下载

     http://download.csdn.net/detail/zx13525079024/4167121

 

  

 

 

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
一、软件开发环境以及开发工具: 框架:.NET Framework 4.0 工具:Visual Studio 2017 插件:DevExpress 18.1.7 环境:IIS 7 二、实现步骤 (1)在项目中创建一个名为WinformAutoUpdate.APP的Winform窗体应用工程,并将默认的Form1.cs窗体文件重命名为MainFrm.cs作为主程序窗体 创建主程序窗体 (2)在项目中再创建一个名为AutoUpdateTask的Winform应用程序工程,并将默认的Form1.cs窗体文件重命名为AutoUpdateTaskFrm.cs作为更新程序窗体 创建更新程序窗体 (3)在更新程序窗体中放入图上所示的相关控件; 进度条控件用于显示更新进度,放入一个Button按钮控件用于用户根据提示进行操作! 实现思路: 1、将更新程序放入磁盘的目录下面,并将其放在已经发布了的IIS中 当用户在运行主程序窗体并点击左上角的更新按钮时,弹出程序更新窗体,并先自动从IIS中拉取updateList.xml文件,然后与本地程序作对比,检测是否需要升级软件; 如果有新版本发布,则点击“立即更新”按钮,程序将从IIS中拉取新发布的ZIP软件包,并自动解压到主程序目录中,覆盖主程序目录中的相关文件(这里值得注意的是,在解压程序之前,我们需要先结束主程序的进程,不然会因主程序进程正在使用而导致报错,另外,我们用到的插件是ICSharpCode.SharpZipLib.dll第三方动态链接库,网上有现成的,可以直接Down下来用);
以下是C# WinForms实现自动更新的示例代码: 1.首先在应用程序中添加一个版本文件,包含当前版本号和最新版本号。 2.创建一个WebClient对象,下载最新的版本文件。 3.将下载的版本文件解析为XML,获取最新版本号。 4.将当前版本号与最新版本号进行比较,如果当前版本号小于最新版本号,则提示用户更新。 5.如果用户同意更新,则使用WebClient对象下载最新版本的安装程序,并启动安装程序。 示例代码: ```csharp using System; using System.ComponentModel; using System.Xml; using System.Net; using System.Windows.Forms; namespace AutoUpdater { public partial class MainForm : Form { private string _versionUrl = "http://example.com/version.xml";//版本文件下载地址 private string _setupUrl = "http://example.com/setup.exe";//安装程序下载地址 public MainForm() { InitializeComponent(); } private void MainForm_Load(object sender, EventArgs e) { CheckUpdate(); } private void CheckUpdate() { try { WebClient client = new WebClient(); client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(VersionDownloadCompleted); client.DownloadStringAsync(new Uri(_versionUrl)); } catch (Exception ex) { MessageBox.Show("更新检查失败:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void VersionDownloadCompleted(object sender, DownloadStringCompletedEventArgs e) { if (e.Error != null) { MessageBox.Show("更新检查失败:" + e.Error.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } try { XmlDocument doc = new XmlDocument(); doc.LoadXml(e.Result); string currentVersion = Application.ProductVersion; string latestVersion = doc.SelectSingleNode("/version/latest").InnerText; if (latestVersion.CompareTo(currentVersion) > 0) { DialogResult result = MessageBox.Show("发现新版本:" + latestVersion + ",是否更新?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { DownloadSetup(); } } else { MessageBox.Show("当前已是最新版本。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception ex) { MessageBox.Show("更新检查失败:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void DownloadSetup() { try { WebClient client = new WebClient(); client.DownloadFileCompleted += new AsyncCompletedEventHandler(SetupDownloadCompleted); client.DownloadFileAsync(new Uri(_setupUrl), "setup.exe"); } catch (Exception ex) { MessageBox.Show("下载安装程序失败:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void SetupDownloadCompleted(object sender, AsyncCompletedEventArgs e) { if (e.Error != null) { MessageBox.Show("下载安装程序失败:" + e.Error.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } try { System.Diagnostics.Process.Start("setup.exe"); Application.Exit(); } catch (Exception ex) { MessageBox.Show("启动安装程序失败:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值