C# 软件自动更新程序(六)

9 篇文章 0 订阅
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;

namespace AutoUpgrade.BL
{
    public class DownloadManage
    {
        private WebClient client = null;
        public DownloadManage(WebClient _client)
        {
            client = _client;
        }
        /// <summary> 
        /// 下载文件 
        /// </summary> 
        /// <param name="arry">下载序号</param> 
        public void DownloadFile(string urlStr, string startupPath)
        {
            try
            {
                this.client.DownloadFileAsync(
                    new Uri(urlStr),
                    startupPath);
            }
            catch (WebException ex)
            {
                throw ex;
            }
        }


        /// <summary> 
        /// 转换字节大小 
        /// </summary> 
        /// <param name="byteSize">输入字节数</param> 
        /// <returns>返回值</returns> 
        public static string ConvertSize(long byteSize)
        {
            string str = "";
            float tempf = (float)byteSize;
            if (tempf / 1024 > 1)
            {
                if ((tempf / 1024) / 1024 > 1)
                {
                    str = ((tempf / 1024) / 1024).ToString("##0.00", CultureInfo.InvariantCulture) + "MB";
                }
                else
                {
                    str = (tempf / 1024).ToString("##0.00", CultureInfo.InvariantCulture) + "KB";
                }
            }
            else
            {
                str = tempf.ToString(CultureInfo.InvariantCulture) + "B";
            }
            return str;
        } 

    }
}

using System;using System.Collections.Generic;using System.ComponentModel;using System.Linq;using System.Net;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using AutoUpgrade.Models;using System.IO;using System.Xml.Linq;using System.Globalization;using System.Configuration;namespace AutoUpgrade.BL{ public class AutoUpgradeBO { private WebClient downWebClient = new WebClient(); private static UpdataFileModel[] updataFileList; private static UpdataFileModel updataFile; private static string fileName;//当前文件名 private static int fileCount;//文件总数 private static int num;//已更新文件数 private static long size;//所有文件大小 private static long upsize;//已更新文件大小 private static long filesize=0;//当前文件大小 private static AutoUpgradeBO _instance = null; public static AutoUpgradeBO Instance() { if (_instance == null) { _instance = new AutoUpgradeBO(); } return _instance; } /// <summary> /// 开始更新 /// </summary> public void UpdaterStart() { //委托下载数据时事件 this.downWebClient.DownloadProgressChanged+=downWebClient_DownloadProgressChanged; //委托下载完成时事件 this.downWebClient.DownloadFileCompleted += downWebClient_DownloadFileCompleted; num = 0; upsize = 0; updataFileList= XmlFileHandle.Instance().GetLocalUpdateFileAll(); if (updataFileList != null) { fileCount = updataFileList.Length; size = updataFileList.Sum(p => p.FileSize); DownloadFile(0); } } /// <summary> /// 关闭程序 /// </summary> public static void UpdaterClose() { try { System.Diagnostics.Process.Start(Application.StartupPath + "\\" + ConfigurationManager.AppSettings["MainApp"]); } catch (Win32Exception ex) { MeBox(ex.Message); } Application.Exit(); } /// <summary> /// 下载文件 /// </summary> /// <param name="arry">下载序号</param> private void DownloadFile(int arry) { try { updataFile = updataFileList[arry]; num++; fileName = updataFile.FileName; DownloadManage bo = new DownloadManage(downWebClient); bo.DownloadFile(updataFile.UrlPath, updataFile.LocalPath); } catch (WebException ex) { MeBox(ex.Message); } } void downWebClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) { DownloadProgressChangedMessageFunction( new DownloadProgressChangedMessageEventArgs{ DownloadProgressChangedEventArgs = e, CurrentFileName = fileName, Size=size, UpSize = upsize }); } void downWebClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs ex) { if (ex.Error != null) { string mes = ex.Error.Message + "\n "+(((System.Net.WebException)(ex.Error)).Response).ResponseUri; MeBox(mes); } else { upsize += filesize; if (updataFileList.Length > num) { DownloadFileCompletedMessageFunction( new DownloadFileCompletedMessageEventArgs { AsyncCompletedEventArgs = ex, CurrUpdataFile = updataFileList[num], Num = num, FileCount = fileCount, Size = size }); DownloadFile(num); } else { UpdaterClose(); } } } /// <summary> /// 弹出提示框 /// </summary> /// <param name="txt">输入提示信息</param> public static void MeBox(string txt) { MessageBox.Show( txt, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); } #region DownloadProgressChangedMessage public event EventHandler<DownloadProgressChangedMessageEventArgs> DownloadProgressChangedMessage; public class DownloadProgressChangedMessageEventArgs : EventArgs { public DownloadProgressChangedEventArgs DownloadProgressChangedEventArgs { get; set; } /// <summary> /// 当前更新的文件对象 /// </summary> public string CurrentFileName { get; set; } /// <summary> /// 已更新文件大小 /// </summary> public long UpSize { get; set; } /// <summary> /// 所有文件大小 /// </summary> public long Size { get; set; } /// <summary> /// 文件总数 /// </summary> public int FileCount { get; set; } } public void DownloadProgressChangedMessageFunction(DownloadProgressChangedMessageEventArgs args) { if (DownloadProgressChangedMessage != null) { DownloadProgressChangedMessage(this, args); } } #endregion #region DownloadFileCompletedMessage public event EventHandler<DownloadFileCompletedMessageEventArgs> DownloadFileCompletedMessage; public class DownloadFileCompletedMessageEventArgs : EventArgs { public AsyncCompletedEventArgs AsyncCompletedEventArgs { get; set; } /// <summary> /// 当前更新的文件对象 /// </summary> public UpdataFileModel CurrUpdataFile { get; set; } /// <summary> /// 所有文件大小 /// </summary> public long Size { get; set; } /// <summary> /// 已更新文件数 /// </summary> public long Num { get; set; } /// <summary> /// 文件总数 /// </summary> public int FileCount { get; set; } } public void DownloadFileCompletedMessageFunction(DownloadFileCompletedMessageEventArgs args) { if (DownloadFileCompletedMessage != null) { DownloadFileCompletedMessage(this, args); } } #endregion }}

 
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值