asp.Net(c#)プログラムバージョンアップグレード更新の実装コード

7254 ワード

直接コード:
 
  
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.IO;
using System.Net;
using System.Xml;
namespace Update
{
    ///
    ///
    ///

    public delegate void UpdateState();
    ///
    ///
    ///

    public class SoftUpdate
    {
        private string download;
private const string updateUrl = "//www.jb51.net/update.xml";// XML
        #region
        public SoftUpdate() { }
        ///
        ///
        ///

        ///
        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;
        ///
        ///
        ///

        public bool IsUpdate
        {
            get
            {
                checkUpdate();
                return isUpdate;
            }
        }
        ///
        ///
        ///

        public string LoadFile
        {
            get { return loadFile; }
            set { loadFile = value; }
        }
        ///
        ///
        ///

        public string NewVerson
        {
            get { return newVerson; }
        }
///
///
///

public string SoftName
{
get { return softName; }
set { softName = value; }
}
        #endregion
        ///
        ///
        ///

        public event UpdateState UpdateFinish;
        private void isFinish() {
            if(UpdateFinish != null)
                UpdateFinish();
        }
        ///
        ///
        ///

        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(" , !");
}
        }
        ///
        ///
        ///

        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(" , !");
            }
        }
        ///
        ///
        ///

        ///
        public override string ToString()
        {
            return this.loadFile;
        }
    }
}

コードを1つのクラスライブラリファイルにコンパイルし、プログラムで引用すればOKです.
入力されたパラメータにはコメントがあります.
以下は更新されたXMLファイルクラス容量であり,空間上に伝えればよいので,XMLファイルのアドレスを得る.
 
  



1.0.1.2
//www.jb51.net/BlogWrite.rar



プログラム更新呼び出し方法:
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);
        }
    }
}

さあ、プログラム全体はこれで終わります.何か正しくない点や疑問があればメッセージをください.