C#實(shí)現(xiàn)在線更新軟件
更新時(shí)間:2015年05月25日 15:11:16 投稿:hebedich
winform程序相對(duì)web程序而言,功能更強(qiáng)大,編程更方便,但軟件更新卻相當(dāng)麻煩,要到客戶端一臺(tái)一臺(tái)地升級(jí),面對(duì)這個(gè)實(shí)際問題,在最近的一個(gè)小項(xiàng)目中,本人設(shè)計(jì)了一個(gè)通過軟件實(shí)現(xiàn)自動(dòng)升級(jí)技術(shù)方案,彌補(bǔ)了這一缺陷,有較好的參考價(jià)值
通過某些手段后臺(tái)更新軟件。首先你要有一個(gè)放置新版本信息的網(wǎng)站
UpdateSoftwareForm.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 CCWin;
using System.Net;
using System.Collections;
using System.IO;
using System.Xml;
using System.Diagnostics;
using System.Threading;
namespace WriteBook
{
public partial class UpdateSoftwareForm : Skin_Metro
{
public UpdateSoftwareForm()
{
InitializeComponent();
}
#region 一些對(duì)象和變量
//使用WebClient下載
WebClient client = new WebClient();
ArrayList downlist = new ArrayList();
//當(dāng)前版本
string nowversion = null;
//最新版本
string latesversion = null;
#endregion
#region 獲取版本號(hào)
/// <summary>
/// 從服務(wù)器上獲取最新的版本號(hào)
/// </summary>
public void DownloadCheckUpdateXml()
{
try
{
//第一個(gè)參數(shù)是文件的地址,第二個(gè)參數(shù)是文件保存的路徑文件名
client.DownloadFile("http://bbs.cloudtour.tk/SoftwareDownload/WriteBook/WriteBook2.xml", "WriteBook2.xml");
}
catch
{
MessageBox.Show("沒有檢測(cè)到更新。", "提示");
this.Close();
}
}
/// <summary>
/// 獲取本地軟件的版本號(hào)
/// </summary>
private void NowVersion()
{
nowversion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString() + "\n";
LocalText.Text = nowversion;
}
/// <summary>
/// 讀取從服務(wù)器獲取的最新版本號(hào)
/// </summary>
public void LatestVersion()
{
try
{
if (File.Exists("WriteBook2.xml.xml"))
{
XmlDocument doc = new XmlDocument();
//加載要讀取的XML
doc.Load("WriteBook2.xml.xml");
//獲得根節(jié)點(diǎn)
XmlElement WriteBook = doc.DocumentElement;
//獲得子節(jié)點(diǎn) 返回節(jié)點(diǎn)的集合
XmlNodeList Update = WriteBook.ChildNodes;
foreach (XmlNode item in Update)
{
latesversion = item.InnerText;
}
LatestText.Text = latesversion;
}
else
{
MessageBox.Show("沒有檢測(cè)到更新。", "提示");
this.Close();
}
}
catch
{
this.Close();
}
}
#endregion
#region 初始化程序
/// <summary>
/// 初始化程序
/// </summary>
private void InitializeandInstall()
{
UpdateProgressBar.Value = 20;
DownloadCheckUpdateXml();
UpdateProgressBar.Value = 40;
NowVersion();
UpdateProgressBar.Value = 60;
LatestVersion();
UpdateProgressBar.Value = 80;
DownloadInstall();
UpdateProgressBar.Value = 100;
}
#endregion
#region 安裝and刪除
/// <summary>
/// 下載安裝包
/// </summary>
public void DownloadInstall()
{
try
{
if (nowversion == latesversion)
{
MessageBox.Show("您已經(jīng)是最新版本。", "提示");
}
else if (nowversion != latesversion && File.Exists("WriteBook2.xml"))
{
MessageBox.Show("發(fā)現(xiàn)新版本,即將下載更新補(bǔ)丁。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
client.DownloadFile("http://bbs.cloudtour.tk/SoftwareDownload/WriteBook/WBsetup.exe", "WBsetup.exe");
if (File.Exists("Setup.exe"))
{
InstallandDelete();
}
else
{
for (int i = 1; i < 3; i++)
{
client.DownloadFile("http://bbs.cloudtour.tk/SoftwareDownload/WriteBook/WBsetup.exe", "WBsetup.exe");
}
MessageBox.Show("下載失敗,請(qǐng)檢查您的網(wǎng)絡(luò)連接是否正常。", "提示");
this.Close();
}
}
}
catch
{
MessageBox.Show("更新失敗,沒有發(fā)現(xiàn)新版本。", "提示");
this.Close();
}
}
/// <summary>
/// 安裝及刪除
/// </summary>
private void InstallandDelete()
{
try
{
DialogResult dr = MessageBox.Show("下載更新成功,是否安裝新更新?", "提示", MessageBoxButtons.YesNoCancel);
if (dr == System.Windows.Forms.DialogResult.Yes)
{
//啟動(dòng)安裝程序
System.Diagnostics.Process.Start("WBsetup.exe");
Thread td = new Thread(JudgeInstall);
td.Start();
}
else { }
}
catch
{
MessageBox.Show("發(fā)生未知錯(cuò)誤,更新失敗。", "提示");
this.Close();
}
}
/// <summary>
/// 判斷安裝進(jìn)程是否存在
/// </summary>
public void JudgeInstall()
{
while (true)
{
Process[] processList = Process.GetProcesses();
foreach (Process process in processList)
{
if (process.ProcessName == "WBsetup.exe") { }
else
{
DialogResult dr = MessageBox.Show("更新成功,是否刪除安裝包?", "提示", MessageBoxButtons.YesNo);
if (dr == System.Windows.Forms.DialogResult.Yes)
{
File.Delete("WBsetup.exe");
File.Delete("WriteBook2.xml");
}
}
}
}
}
#endregion
/// <summary>
/// 點(diǎn)擊初始化程序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void UpdateButton_Click(object sender, EventArgs e)
{
InitializeandInstall();
}
}
}
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
您可能感興趣的文章:
- 用c# 自動(dòng)更新程序
- C# protobuf自動(dòng)更新cs文件
- C#如何讀取Txt大數(shù)據(jù)并更新到數(shù)據(jù)庫詳解
- C# Winform自動(dòng)更新程序?qū)嵗斀?/a>
- c#中Winform實(shí)現(xiàn)多線程異步更新UI(進(jìn)度及狀態(tài)信息)
- c# Winform 程序自動(dòng)更新實(shí)現(xiàn)方法
- C#微信公眾平臺(tái)開發(fā)之a(chǎn)ccess_token的獲取存儲(chǔ)與更新
- C#在子線程中更新窗口部件的寫法
- C#更新SQLServer中TimeStamp字段(時(shí)間戳)的方法
- C#使用Ado.Net更新和添加數(shù)據(jù)到Excel表格的方法
- C#批量更新sql實(shí)例
- c# 使用handle.exe解決程序更新文件被占用的問題
相關(guān)文章
c#橋接模式(bridge結(jié)構(gòu)模式)用法實(shí)例
這篇文章主要介紹了c#橋接模式(bridge結(jié)構(gòu)模式)用法,較為詳細(xì)的分析了橋接模式的原理與用法實(shí)例,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-12-12
sqlserver備份還原數(shù)據(jù)庫功能封裝分享
這篇文章主要介紹了sqlserver備份還原數(shù)據(jù)庫功能封裝示例,需要的朋友可以參考下2014-03-03
C#?Socket數(shù)據(jù)接收的三種實(shí)現(xiàn)方式
本文主要介紹了C#?Socket數(shù)據(jù)接收的三種實(shí)現(xiàn)方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
C#5.0中的異步編程關(guān)鍵字async和await
這篇文章介紹了C#5.0中的異步編程關(guān)鍵字async和await,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
C#編程實(shí)現(xiàn)DataTable添加行的方法
這篇文章主要介紹了C#編程實(shí)現(xiàn)DataTable添加行的方法,結(jié)合兩個(gè)實(shí)例形式分析了C#操作DataTable實(shí)現(xiàn)動(dòng)態(tài)添加行的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11

