一个简单的自动升级程序

最近需要给一个小程序加上一个自动升级功能,由于这种功能比较通用也比较独立,所以发出来,以备后用。

 

 截图如下:

代码如下:

入口函数的时候判断是否需有新版本,有则启动下载窗口,然后启动目标程序。

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Diagnostics;
namespace FileUpdate
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            try
            {
                string verO = "";
                string exe = "ScannerPC.exe";
                string url = "http://localhost:6564/";
                string txt = "Update.txt";

                using(var fs = File.Open(txt, FileMode.OpenOrCreate))
                using (StreamReader read = new StreamReader(fs))
                {
                    verO = read.ReadLine();
                    if (verO == null)
                        verO = "";
                }

                WebClient webClient = new WebClient();
                var Ver = webClient.DownloadString(url + "ver.txt");
                if (Ver != verO)
                {
                    var form = new Form1(Ver,url,exe);
                    Application.Run(form);
                }

                ProcessStartInfo info = new ProcessStartInfo();
                info.FileName = exe;
                info.WindowStyle = ProcessWindowStyle.Normal;
                Process pro = Process.Start(info);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

主窗口使用webclient的DownloadFileAsync异步方法下载http文件(不支持断点续传)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;

namespace FileUpdate
{
    public partial class Form1 : Form
    {
        WebClient webClient = new WebClient();
        string url = "";
        string exePath = "";
        string ver = "";

        public Form1(string ver,string url,string exe)
        {
            this.ver = ver;
            this.url = url;
            exePath = exe;

            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                webClient.DownloadFileCompleted += webClient_DownloadFileCompleted;
                webClient.DownloadProgressChanged += webClient_DownloadProgressChanged;
                webClient.DownloadFileAsync(new Uri(url + exePath), exePath);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Close();
            }
        }

        void webClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
        }

        void webClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            if(e.Error != null)
            {
                MessageBox.Show("下载文件出错:"+e.Error.Message);
            }
            else
            {
                using (var writer = new StreamWriter("Update.txt"))
                {
                    writer.WriteLine(ver);
                }
            }
            Close();
        }
    }
}

 

只支持单个文件更新,没有写多文件支持,也没有加入zip解压这种功能,如果有兴趣可自行添加。可以直接做为程序的启动器,也可以在主程序里面直接调用该程序更新。看起来有点像某些黑客工具的远程下载功能的样子(小),博客文章太少了,只能滥竽充数了o(╯□╰)o

 

源码:下载

转载于:https://www.cnblogs.com/HelliX/p/5885384.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
版本08.11.03,有详细的帮助文档,完全免费。整个程序才80多K,但功能却不少,非常适合免费软件和共享软件作者。有以下功能: 1. 版本号比较(考虑到可能会有数据方面的升级,因此版本号不从程序自身中取,而是根据需要在配置时设定),同一个软件中允许有多个不同版本的程序或数据。 2. 根据版本号比较结果,从服务器中下载相应程序或数据进行升级,然后使用本地与服务器版本号一致。版本号可以分级,用“.”隔开,级数不限,如10或10.01或100.001.002等等。 3. 可以指定单个文件的存放目录,指定的目录若不存在时将会自动生成。 4. 升级时,如果相关的程序在运行,会自动地将其关闭,以保证升级的顺利进行。 5. 升级文件可以事先压缩好,放到服务器上,下载到本地后再自动进行解压。目前只支持ZIP格式。 6. 可以指定升级完成后自动启动一应用程序,有两种启动模式(常规与强制),同时还可以根据具体情况决定是否需要执行此应用程序。 7. 可以自定义标题栏图标(可以为动画),窗口右侧的图像。 8. 可以自定义自己的提示语(升级启动前后的提示语)。 9. 提供了常规显示、静默显示、无显示三种进度显示方式。 10. 可以指定升级后是否修改相关项的版本号。若设置为不修改,则可以使应用程序每次启动时都能从网络获取相关内容。 11. 应用程序可以通过消息方式获取自动升级完成后的结果。 12. 从版本V08.11.01开始,本软件可以进行自我更新。以后你只要下载最新的版本,放在你的服务器指定位置即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值