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

本文详细介绍了如何使用C#编程实现软件的自动更新功能,涵盖了从检查更新到下载安装的完整流程,帮助开发者提升用户体验。
摘要由CSDN通过智能技术生成
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoUpgrade.Models;
using System.Xml.Linq;
using System.Net;
using System.Windows.Forms;
using System.Configuration;
using System.IO;
using System.Diagnostics;

namespace AutoUpgrade.BL
{
    public class XmlFileHandle
    {
        public  List<UpdataFileModel> updataFileList = null;

        public List<UpdataFileModel> interUpdataFileList = null;
        public List<UpdataFileModel> localUpdataFileList = null;

        private string localRootPath = string.Empty;
        private string ReleaseAddress = string.Empty;
         private string InternetUpdateFile = string.Empty;
        private string LocalUpdateFile = string.Empty;

        private static XmlFileHandle _instance = null;
        public static XmlFileHandle Instance()
        {
            if (_instance == null)
            {
                _instance = new XmlFileHandle();
            }
            return _instance;
        }

        public XmlFileHandle()
        {
            localRootPath = Application.StartupPath;
            ReleaseAddress = ConfigurationManager.AppSettings["ReleaseAddress"];
            InternetUpdateFile = ConfigurationManager.AppSettings["InternetUpdateFile"];
            LocalUpdateFile = ConfigurationManager.AppSettings["LocalUpdateFile"];

        }

        #region 检查更新文件是否有新的补丁

        /// <summary>
        /// 检查更新文件是否有新的补丁
        /// </summary>
        /// <returns></returns>
        public bool CheckUpdateFile()
        {
            DateTime localupDate = GetLocalUpdateFileReleaseTime();
            DateTime internetUpDate = GetInterUpdateFileReleaseTime();
            if (DateTime.Compare(internetUpDate, localupDate) > 0)
            {
                LoadLocalUpdateFileAll();
                return true;
            }
            return false;

            

        }

        private DateTime GetLocalUpdateFileReleaseTime()
        {
            string path = localRootPath + "\\" + LocalUpdateFile;
            return GetUpdateFileReleaseTime(path);
        }

        private DateTime GetInterUpdateFileReleaseTime()
        {
            string path = InternetUpdateFile;
            return GetUpdateFileReleaseTime(path);
        }

        private DateTime GetUpdateFileReleaseTime(string strpath)
        {
            if (string.IsNullOrEmpty(strpath))
                return DateTime.MinValue;
            try
            {
                XElement root = XElement.Load(strpath);
                if (!string.IsNullOrEmpty(root.Attribute("ReleaseTime").Value))
                    return Convert.ToDateTime(root.Attribute("ReleaseTime").Value);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return DateTime.MinValue;
        }
        #endregion        

        #region 读取更新文件到数据集合List<FolderInfoModel>中
        /// <summary> 
        /// 获取Internet文件列表并下载 
        /// </summary> 
        private List<FolderInfoModel> ReadInternetUpdateList()
        {
            string path = InternetUpdateFile;
            return  ReadUpdateList(path);
        }

        /// <summary>
        /// 获取Local文件列表
        /// </summary>
        /// <returns></returns>
        private List<FolderInfoModel> ReadLocalUpdateList()
        {
            string path = localRootPath + "\\" + LocalUpdateFile;
            return ReadUpdateList(p
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值