asp.net(c#)程式版本升級更新的實現代碼

來源:互聯網
上載者:User

直接上代碼: 複製代碼 代碼如下:using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.IO;
using System.Net;
using System.Xml;
namespace Update
{
/// <summary>
/// 更新完成觸發的事件
/// </summary>
public delegate void UpdateState();
/// <summary>
/// 程式更新
/// </summary>
public class SoftUpdate
{
private string download;
private const string updateUrl = "http://www.jb51.net/update.xml";//升級配置的XML檔案地址
#region 建構函式
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>
/// 更新完成時觸發的事件
/// </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("更新出現錯誤,網路連接失敗!");
}
}
/// <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("更新出現錯誤,請確認網路連接無誤後重試!");
}
}
/// <summary>
/// 擷取要更新的檔案
/// </summary>
/// <returns></returns>
public override string ToString()
{
return this.loadFile;
}
}
}

把代碼編譯為一個類庫檔案,通過程式引用就OK啦。
傳入的參數已經有注釋了。
下面是更新的XML檔案類容,傳到空間上面就可以了,得到XML檔案的地址。 複製代碼 代碼如下:<?xml version="1.0" encoding="utf-8" ?>
<Update>
<Soft Name="BlogWriter">
<Verson>1.0.1.2</Verson>
<DownLoad>http://www.jb51.net/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);
}
}
}

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.