c# 自动更新设计

来源:互联网 发布:qq网络背景音乐链接 编辑:程序博客网 时间:2024/05/18 02:30

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Net;
namespace AutoUpdate
{
    public class AutoUpdateConfig
    {
        public string autoUpdateUrl { get; set; }
        public string Version { get; set; }
        public string Description { get; set; }
        public int fileCount { get; set; }
        public string filePath { get; set; }
        public List<AutoUpdateFile> fileList { get; set; }
    }
    public class AutoUpdateFile
    {
        /// <summary>
        /// 文件名
        /// </summary>
        public string fileName { get; set; }
        /// <summary>
        /// 文件大小
        /// </summary>
        public string fileSize { get; set; }
        /// <summary>
        /// true 表示成功下载
        /// </summary>
        public string  status { get; set; }
       
    }
    public class AutoUpdate
    {
        public AutoUpdateConfig GetAutoUpdateConfig(string autoUpdateUrl)
        {

            AutoUpdateConfig model = null;
            XmlDocument doc = new XmlDocument();
            try
            {
                doc.Load(new WebClient().OpenRead(autoUpdateUrl));
            }
            catch(Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message);
                return model;
            }
            model = new AutoUpdateConfig();
            XmlElement root = doc.DocumentElement;
            model.Version = root.SelectSingleNode("version").InnerText.Trim();
            model.Description = root.SelectSingleNode("description").InnerText;
            model.autoUpdateUrl = autoUpdateUrl;
            List<AutoUpdateFile> list = new List<AutoUpdateFile>();
            foreach (XmlNode node in root.SelectSingleNode("filelist").ChildNodes)
            {
                AutoUpdateFile item = new AutoUpdateFile();
                item.fileName = node.Attributes["name"].InnerText;
                item.fileSize = node.Attributes["size"].InnerText;
                item.status = "";
                list.Add(item);
            }
            model.fileList = list;
            model.fileCount = list.Count;
            return model;
        }
        public void formatVersion(ref  string ver1, ref   string ver2)
        {
            ver1 = ver1.Replace(".", "");
            ver2 = ver2.Replace(".", "");
            if (ver1.Length < ver2.Length)
            {
                ver1 = ver1.PadRight(ver2.Length, '0');
            }
            else
            {
                ver2 = ver2.PadRight(ver1.Length, '0');
            }

        }

    }
}

=============================frmMain.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Net;
using System.Diagnostics;
using System.Reflection;
namespace AutoUpdate
{
    public partial class frmMain : DevExpress.XtraEditors.XtraForm
    {
        AutoUpdate dal = new AutoUpdate();
        AutoUpdateConfig model;
        string autoUpdateurl { get; set; }
        string processName { get; set; }
        public frmMain(string autoUpdateUrl, string processName)
        {
            InitializeComponent();

            this.autoUpdateurl = autoUpdateUrl;
            this.processName = processName;
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            txtDescription.Visible = true;
            gridControl1.Visible = false;
            btnUpdate.Text = "下一步";
            model = dal.GetAutoUpdateConfig(autoUpdateurl);

            if (model == null)
            {
                btnUpdate.Enabled = false;
                txtDescription.Text = autoUpdateurl+"\r\n远程服务器错误";

            }
            else
            {
                txtDescription.Text = model.Description;
                string version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
                string ver1 = version;
                string ver2 = model.Version;
                dal.formatVersion(ref ver1, ref ver2);
                if (Convert.ToInt32(ver1) >= Convert.ToInt32(ver2))
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("当前已是最新版本无需更新", "提示");
                }
                if (model != null)
                {
                    gridControl1.DataSource = model.fileList;
                }
            }
        }
        void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            AutoUpdateFile item = (AutoUpdateFile)e.UserState;
            if (e.Error != null)
            {
               item.status = "下载失败";
               gridView1.RefreshData();
            }
            else
            {
                //model.fileList.FirstOrDefault(u => u.fileName == item.fileName).status = "成功";
                item.status = "成功";
                gridView1.RefreshData();
                if (model.fileList.FirstOrDefault(u => u.status != "成功") == null)
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("已经成功升级到" + model.Version + ",请重新打开wms系统", "提示");
                    this.Text = "已经成功升级到" + model.Version ;
                    btnUpdate.Visible = false;
                }
            }
        }
        void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            AutoUpdateFile item = (AutoUpdateFile)e.UserState;
            item.status= (e.BytesReceived / Convert.ToDecimal(e.TotalBytesToReceive)).ToString("P");
            gridView1.RefreshData();
        }
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (txtDescription.Visible)
            {
                if (Process.GetProcessesByName(this.processName).Length > 0)
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("请关闭wms主系统", "警告");
                    return;
                }

                gridControl1.Visible = true;
                txtDescription.Visible = false;
                btnUpdate.Text = "升级";
             
            }
            else
            {
                for (int i = 0; i < gridView1.RowCount; i++)
                {
                    AutoUpdateFile item = (AutoUpdateFile)gridView1.GetRow(i);
                    using (WebClient client = new WebClient())
                    {
                        string fileName = item.fileName;
                        client.DownloadFileAsync(new Uri(new Uri(model.autoUpdateUrl), fileName), fileName, item);
                        client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
                        client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
                    }
                }
            }
           
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

      
    }
}


 

原创粉丝点击