基本信息
源码名称:客户端程序(winform)发现新版本,自动升级程序(思路+源码)
源码大小:2.31M
文件格式:.rar
开发语言:C#
更新时间:2019-03-05
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using AutoUpdateClient.WebReference;
using System.Threading;

namespace AutoUpdateClient
{
    public partial class FrmAutoUpdate : Form
    {
        public FrmAutoUpdate()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
        }

        UpdateHelper _UpdateObj;
        /// <summary>
        /// 是否窗体加载时自动开始更新
        /// </summary>
        public bool IsAutoStartUpdate { set; get; }
        /// <summary>
        /// 更新完成之后是否退出程序
        /// </summary>
        public bool IsUpdatedClose { set; get; }
        /// <summary>
        /// 写当前运行状态到控件
        /// </summary>
        void WriteState(string msg, params object[] args)
        {
            string msgText = string.Format(msg, args);
            lboxDetail.Text = msgText;
            lboxDetail.Items.Insert(0, msgText   "[ "   DateTime.Now.ToString("HH:mm:ss")   " ] ");
        }
        /// <summary>
        /// 开始更新
        /// </summary>
        private void btnStartUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                btnStartUpdate.Enabled = false;
                FileInfoModel[] fileModels = _UpdateObj.GetCanUpdateFile();
                WriteState("总共需要更新文件{0}个", fileModels.Length);
                int downCount = _UpdateObj.StartUpdate();
                if (downCount == 0)
                {
                    WriteState("没有要更新的文件");
                }
                progressBar.Maximum = downCount;
            }
            catch (Exception ex)
            {
                MessageBox.Show("下载数据时出错,原因:"   ex.Message);
            }
        }
        /// <summary>
        /// 窗体加载
        /// </summary>
        private void FrmMain_Load(object sender, EventArgs e)
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(a =>
            {
                _UpdateObj = new UpdateHelper();
                _UpdateObj.DownloadFileBeforeHandle  = UpdateObj_DownloadFileBeforeHandle;
                _UpdateObj.DownloadFileCountHandle  = UpdateObj_DownloadFileCountHandle;
                _UpdateObj.DownloadFileAfterHandle  = UpdateObj_DownloadFileAfterHandle;
                _UpdateObj.DownloadOverHandle  = UpdateObj_DownloadOverHandle;
                _UpdateObj.DownloadErrorHandle  = UpdateObj_DownloadErrorHandle;
                if (IsAutoStartUpdate)
                {
                    btnStartUpdate_Click(sender, e);
                }
            }));
        }
        void UpdateObj_DownloadFileBeforeHandle(UpdateHelper upObj, FileInfoModel fileModel)
        {
            WriteState("正在下载文件{0}", fileModel.FileName);
        }
        void UpdateObj_DownloadFileCountHandle(UpdateHelper upObj, FileInfoModel fileModel)
        {
            if (progressBar.Value < progressBar.Maximum)
                progressBar.Value  ;
        }
        void UpdateObj_DownloadFileAfterHandle(UpdateHelper upObj, FileInfoModel fileModel)
        {
            WriteState("下载文件{0}完成", fileModel.FileName);
        }
        void UpdateObj_DownloadOverHandle(UpdateHelper upObj, FileInfoModel fileModel)
        {
            WriteState("文件全部下载完成!");
            string mainAppFile = System.Configuration.ConfigurationManager.AppSettings["MainApp"];
            if (!string.IsNullOrEmpty(mainAppFile))
            {
                Process.Start(mainAppFile);
            }
            if (IsUpdatedClose)
            {
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }

        }
        void UpdateObj_DownloadErrorHandle(UpdateHelper upObj, FileInfoModel fileModel)
        {
            WriteState("下载文件{0}异常,原因:{1}", fileModel.FileName, upObj.DownloadException.Message);
        }
    }
}