C#完成在線更新軟件。本站提示廣大學習愛好者:(C#完成在線更新軟件)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成在線更新軟件正文
經由過程某些手腕後台更新軟件。起首你要有一個放置新版本信息的網站
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 一些對象和變量
//應用WebClient下載
WebClient client = new WebClient();
ArrayList downlist = new ArrayList();
//以後版本
string nowversion = null;
//最新版本
string latesversion = null;
#endregion
#region 獲得版本號
/// <summary>
/// 從辦事器上獲得最新的版本號
/// </summary>
public void DownloadCheckUpdateXml()
{
try
{
//第一個參數是文件的地址,第二個參數是文件保留的途徑文件名
client.DownloadFile("http://bbs.cloudtour.tk/SoftwareDownload/WriteBook/WriteBook2.xml", "WriteBook2.xml");
}
catch
{
MessageBox.Show("沒有檢測到更新。", "提醒");
this.Close();
}
}
/// <summary>
/// 獲得當地軟件的版本號
/// </summary>
private void NowVersion()
{
nowversion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString() + "\n";
LocalText.Text = nowversion;
}
/// <summary>
/// 讀取從辦事器獲得的最新版本號
/// </summary>
public void LatestVersion()
{
try
{
if (File.Exists("WriteBook2.xml.xml"))
{
XmlDocument doc = new XmlDocument();
//加載要讀取的XML
doc.Load("WriteBook2.xml.xml");
//取得根節點
XmlElement WriteBook = doc.DocumentElement;
//取得子節點 前往節點的聚集
XmlNodeList Update = WriteBook.ChildNodes;
foreach (XmlNode item in Update)
{
latesversion = item.InnerText;
}
LatestText.Text = latesversion;
}
else
{
MessageBox.Show("沒有檢測到更新。", "提醒");
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("您曾經是最新版本。", "提醒");
}
else if (nowversion != latesversion && File.Exists("WriteBook2.xml"))
{
MessageBox.Show("發明新版本,行將下載更新補釘。", "提醒", 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("下載掉敗,請檢討您的收集銜接能否正常。", "提醒");
this.Close();
}
}
}
catch
{
MessageBox.Show("更新掉敗,沒有發明新版本。", "提醒");
this.Close();
}
}
/// <summary>
/// 裝置及刪除
/// </summary>
private void InstallandDelete()
{
try
{
DialogResult dr = MessageBox.Show("下載更新勝利,能否裝置新更新?", "提醒", MessageBoxButtons.YesNoCancel);
if (dr == System.Windows.Forms.DialogResult.Yes)
{
//啟動裝置法式
System.Diagnostics.Process.Start("WBsetup.exe");
Thread td = new Thread(JudgeInstall);
td.Start();
}
else { }
}
catch
{
MessageBox.Show("產生未知毛病,更新掉敗。", "提醒");
this.Close();
}
}
/// <summary>
/// 斷定裝置過程能否存在
/// </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>
/// 點擊初始化法式
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void UpdateButton_Click(object sender, EventArgs e)
{
InitializeandInstall();
}
}
}
以上所述就是本文的全體內容了,願望年夜家可以或許愛好。