• <fieldset id="8imwq"><menu id="8imwq"></menu></fieldset>
  • <bdo id="8imwq"><input id="8imwq"></input></bdo>
    最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
    問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
    當前位置: 首頁 - 科技 - 知識百科 - 正文

    asp.net(c#)程序版本升級更新的實現(xiàn)代碼

    來源:懂視網 責編:小采 時間:2020-11-27 22:43:18
    文檔

    asp.net(c#)程序版本升級更新的實現(xiàn)代碼

    asp.net(c#)程序版本升級更新的實現(xiàn)代碼:直接上代碼: 代碼如下:using System; using System.Collections.Generic; using System.Text; using System.Reflection; using System.IO; using System.Net; using System.Xml; namespace Update
    推薦度:
    導讀asp.net(c#)程序版本升級更新的實現(xiàn)代碼:直接上代碼: 代碼如下:using System; using System.Collections.Generic; using System.Text; using System.Reflection; using System.IO; using System.Net; using System.Xml; namespace Update

    直接上代碼:
    代碼如下:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Reflection;
    using System.IO;
    using System.Net;
    using System.Xml;
    namespace Update
    {
        /// <summary>
        /// 更新完成觸發(fā)的事件
        /// </summary>
        public delegate void UpdateState();
        /// <summary>
        /// 程序更新
        /// </summary>
        public class SoftUpdate
        {
            private string download;
    private const string updateUrl = "http://www.gxlcms.com/update.xml";//升級配置的XML文件地址
            #region 構造函數(shù)
            public SoftUpdate() { }
            /// <summary>
            /// 程序更新
            /// </summary>
            /// <param name="file">要更新的文件</param>
            public SoftUpdate(string file,string softName) {
                this.LoadFile = file;
    this.SoftName = softName;
            }
            #endregion
            #region 屬性
            private string loadFile;
            private string newVerson;
    private string softName;
            private bool isUpdate;
            /// <summary>
            /// 或取是否需要更新
            /// </summary>
            public bool IsUpdate
            {
                get
                {
                    checkUpdate();
                    return isUpdate;
                }
            }
            /// <summary>
            /// 要檢查更新的文件
            /// </summary>
            public string LoadFile
            {
                get { return loadFile; }
                set { loadFile = value; }
            }
            /// <summary>
            /// 程序集新版本
            /// </summary>
            public string NewVerson
            {
                get { return newVerson; }
            }
    /// <summary>
    /// 升級的名稱
    /// </summary>
    public string SoftName
    {
    get { return softName; }
    set { softName = value; }
    }
            #endregion
            /// <summary>
            /// 更新完成時觸發(fā)的事件
            /// </summary>
            public event UpdateState UpdateFinish;
            private void isFinish() {
                if(UpdateFinish != null)
                    UpdateFinish();
            }
            /// <summary>
            /// 下載更新
            /// </summary>
            public void Update()
            {
    try
    {
    if (!isUpdate)
    return;
    WebClient wc = new WebClient();
    string filename = "";
    string exten = download.Substring(download.LastIndexOf("."));
    if (loadFile.IndexOf(@"\") == -1)
    filename = "Update_" + Path.GetFileNameWithoutExtension(loadFile) + exten;
    else
    filename = Path.GetDirectoryName(loadFile) + "\\Update_" + Path.GetFileNameWithoutExtension(loadFile) + exten;
    wc.DownloadFile(download, filename);
    wc.Dispose();
    isFinish();
    }
    catch
    {
    throw new Exception("更新出現(xiàn)錯誤,網絡連接失敗!");
    }
            }
            /// <summary>
            /// 檢查是否需要更新
            /// </summary>
            public void checkUpdate()
            {
                try {
                    WebClient wc = new WebClient();
                    Stream stream = wc.OpenRead(updateUrl);
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.Load(stream);
                    XmlNode list = xmlDoc.SelectSingleNode("Update");
                    foreach(XmlNode node in list) {
                        if(node.Name == "Soft" && node.Attributes["Name"].Value.ToLower() == SoftName.ToLower()) {
                            foreach(XmlNode xml in node) {
                                if(xml.Name == "Verson")
                                    newVerson = xml.InnerText;
                                else
                                    download = xml.InnerText;
                            }
                        }
                    }
                    Version ver = new Version(newVerson);
                    Version verson = Assembly.LoadFrom(loadFile).GetName().Version;
                    int tm = verson.CompareTo(ver);
                    if(tm >= 0)
                        isUpdate = false;
                    else
                        isUpdate = true;
                }
                catch(Exception ex) {
    throw new Exception("更新出現(xiàn)錯誤,請確認網絡連接無誤后重試!");
                }
            }
            /// <summary>
            /// 獲取要更新的文件
            /// </summary>
            /// <returns></returns>
            public override string ToString()
            {
                return this.loadFile;
            }
        }
    }

    把代碼編譯為一個類庫文件,通過程序引用就OK啦。
    傳入的參數(shù)已經有注釋了。
    下面是更新的XML文件類容,傳到空間上面就可以了,得到XML文件的地址。
    代碼如下:

    <?xml version="1.0" encoding="utf-8" ?>
    <Update>
    <Soft Name="BlogWriter">
    <Verson>1.0.1.2</Verson>
    <DownLoad>//www.gxlcms.com/BlogWrite.rar</DownLoad>
    </Soft>
    </Update>

    程序更新調用方法:
    1、先引用上面的DLL。
    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.Threading;
    using System.Net;
    using System.Xml;
    using Update;
    namespace UpdateTest
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                checkUpdate();
            }
            public void checkUpdate()
            {
    SoftUpdate app = new SoftUpdate(Application.ExecutablePath, "BlogWriter");
                app.UpdateFinish += new UpdateState(app_UpdateFinish);
    try
    {
    if (app.IsUpdate && MessageBox.Show("檢查到新版本,是否更新?", "Update", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
    {
    Thread update = new Thread(new ThreadStart(app.Update));
    update.Start();
    }
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
            }
            void app_UpdateFinish() {
                    MessageBox.Show("更新完成,請重新啟動程序!", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }

    好了,整個程序到此結束。如覺得有哪里不正確或者有疑問的請給我留言。

    聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

    文檔

    asp.net(c#)程序版本升級更新的實現(xiàn)代碼

    asp.net(c#)程序版本升級更新的實現(xiàn)代碼:直接上代碼: 代碼如下:using System; using System.Collections.Generic; using System.Text; using System.Reflection; using System.IO; using System.Net; using System.Xml; namespace Update
    推薦度:
    標簽: 升級 更新 版本
    • 熱門焦點

    最新推薦

    猜你喜歡

    熱門推薦

    專題
    Top
    主站蜘蛛池模板: 久久亚洲精品人成综合网| 91精品国产福利在线观看麻豆| 欧美精品在线免费| 国产精品186在线观看在线播放| 精品无人区无码乱码毛片国产| 国产亚洲福利精品一区| 亚洲国产精品成人久久| 在线精品自拍无码| 亚洲AV成人精品网站在线播放| 99久久综合国产精品二区| 久久久一本精品99久久精品66| 久久久久国产精品三级网| 日韩麻豆国产精品欧美| 久久精品成人影院| 国产69精品久久久久777| 中文成人无码精品久久久不卡 | 国产成人久久精品区一区二区| 亚洲伊人久久精品影院| 精品无码国产一区二区三区AV| 人妻少妇看A偷人无码精品视频| 国产福利电影一区二区三区,亚洲国模精品一区 | 无码精品第一页| 国产99精品一区二区三区免费| 国产2021精品视频免费播放| 国产精品日韩AV在线播放| 亚洲精品午夜无码电影网| 亚洲av午夜精品一区二区三区| 久热精品视频第一页| 精品久久久久久国产免费了| 国产三级精品三级在线观看| 51精品资源视频在线播放| 久久精品www| 国产精品福利在线观看免费不卡| 996久久国产精品线观看| 国产精品亚洲日韩欧美色窝窝色欲 | 老司机亚洲精品影院无码| 亚洲精品制服丝袜四区| 亚洲精品亚洲人成在线观看| 亚洲精品国产成人片| 亚洲国产精品无码久久青草| 日产精品久久久一区二区|