C# Winform自動(dòng)更新程序?qū)嵗斀?/h1>
更新時(shí)間:2021年03月10日 08:31:42 作者:garychk
這篇文章主要為大家詳細(xì)介紹了C# Winform 自動(dòng)更新程序?qū)嵗?,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了C# Winform 自動(dòng)更新程序,供大家參考,具體內(nèi)容如下
第一步:檢查更新
檢查更新其實(shí)無(wú)非就是去比較更新包的版本和本地軟件版本,如果高則更新、低則不更新。怎么獲取版本號(hào)方法很多,本案例是獲取軟件的配置文件。
private bool CheckUpdate()
{
bool result = false;
try
{
string Cfg = TxtRead(exePath "\\Config.txt");
ConfigLocal = JsonConvert.DeserializeObject<DTO_Config>(Cfg);
CheckUpdateURL = ConfigLocal.AutoUpdateURL;
Cfg = TxtRead(CheckUpdateURL "\\Config.txt");
ConfigRemote = JsonConvert.DeserializeObject<DTO_Config>(Cfg);
VersionR = ConfigRemote.Version;
VersionL = ConfigLocal.Version;
int VersionRemote = int.Parse(ConfigRemote.Version.Replace(".", ""));
int VersionLocal = int.Parse(ConfigLocal.Version.Replace(".", ""));
result = VersionRemote > VersionLocal;
}
catch { }
return result;
}
第二步:下載更新包
因?yàn)镃/S的軟件更新是面對(duì)所有用戶(hù),S端除了給C端提供基本的服務(wù)外,還可以給C端提供更新包。而這個(gè)S端可以是網(wǎng)絡(luò)上的一個(gè)固定地址,也可以是局域網(wǎng)內(nèi)一個(gè)公共盤(pán)。那下載更新包無(wú)非就是去訪問(wèn)服務(wù)端的文件,然后Copy下來(lái)或下載下來(lái)。下面給出訪問(wèn)網(wǎng)絡(luò)和訪問(wèn)局域網(wǎng)兩個(gè)案例:
A、訪問(wèn)遠(yuǎn)程網(wǎng)絡(luò)地址這里采用的是WebClient
public void DownLoadFile()
{
if (!Directory.Exists(UpdateFiles))
{
Directory.CreateDirectory(UpdateFiles);
}
using (WebClient webClient = new WebClient())
{
try
{
webClient.DownloadFileCompleted = new AsyncCompletedEventHandler(client_DownloadFileCompleted);
webClient.DownloadProgressChanged = new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
webClient.DownloadFileAsync(new Uri(CheckUpdateURL "\\UpdateFile.rar"), UpdateFiles "\\UpdateFile.rar");
}
catch (WebException ex)
{
MessageBox.Show(ex.Message, "系統(tǒng)提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
這里面應(yīng)用到兩個(gè)方法,DownloadProgressChanged,監(jiān)聽(tīng)異步下載的進(jìn)度;DownloadFileCompleted,監(jiān)聽(tīng)完成異步文件下載;
private void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
this.progressBarUpdate.Minimum = 0;
this.progressBarUpdate.Maximum = (int)e.TotalBytesToReceive;
this.progressBarUpdate.Value = (int)e.BytesReceived;
this.lblPercent.Text = e.ProgressPercentage "%";
}
private void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Error != null)
{
MessageBox.Show(e.Error.Message, "系統(tǒng)提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
this.lblMessage.Text = "下載完成";
//復(fù)制更新文件替換舊文件
DirectoryInfo TheFolder = new DirectoryInfo(UpdateFiles);
foreach (FileInfo NextFile in TheFolder.GetFiles())
{
File.Copy(NextFile.FullName, Application.StartupPath NextFile.Name, true);
}
}
}
B、訪問(wèn)服務(wù)端公共盤(pán),直接采用File.Copy
public void GetRemoteFile()
{
try
{
DirectoryInfo TheFolder = new DirectoryInfo(CheckUpdateURL);
FileInfo[] FileList = TheFolder.GetFiles();
this.progressBarUpdate.Minimum = 0;
this.progressBarUpdate.Maximum = FileList.Length;
foreach (FileInfo NextFile in FileList)
{
if (NextFile.Name != "Config.txt")
{
File.Copy(NextFile.FullName, exePath "\\" NextFile.Name, true);
}
this.lblMessage.Text = "更新" NextFile.Name;
this.progressBarUpdate.Value = 1;
this.lblPercent.Text = "更新進(jìn)度... " (this.progressBarUpdate.Value / FileList.Length) * 100 "%";
}
this.lblMessage.Text = "更新完成";
//更改本地版本號(hào)為最新版本號(hào)
ConfigLocal.Version = VersionR;
string cfgs = JsonConvert.SerializeObject(ConfigLocal);
TxtWrite(Application.StartupPath "\\Config.txt", cfgs);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "系統(tǒng)提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
第三步:替換本地文件
這一步或許在第二步中已經(jīng)實(shí)現(xiàn)了,如果你采用的是File.Copy。替換也就是復(fù)制粘貼的問(wèn)題。采用WebClient下載了zip包,那還需解壓一下壓縮包然后再File.Copy。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
-
用 C# 編寫(xiě)一個(gè)停放在任務(wù)欄上的圖標(biāo)程序
用 C# 編寫(xiě)一個(gè)停放在任務(wù)欄上的圖標(biāo)程序... 2007-03-03
-
C# HttpClient 如何使用 Consul 發(fā)現(xiàn)服務(wù)
這篇文章主要介紹了C# HttpClient 如何使用 Consul 發(fā)現(xiàn)服務(wù),幫助大家更好的理解和使用c#,感興趣的朋友可以了解下 2021-02-02
最新評(píng)論
本文實(shí)例為大家分享了C# Winform 自動(dòng)更新程序,供大家參考,具體內(nèi)容如下
第一步:檢查更新
檢查更新其實(shí)無(wú)非就是去比較更新包的版本和本地軟件版本,如果高則更新、低則不更新。怎么獲取版本號(hào)方法很多,本案例是獲取軟件的配置文件。
private bool CheckUpdate() { bool result = false; try { string Cfg = TxtRead(exePath "\\Config.txt"); ConfigLocal = JsonConvert.DeserializeObject<DTO_Config>(Cfg); CheckUpdateURL = ConfigLocal.AutoUpdateURL; Cfg = TxtRead(CheckUpdateURL "\\Config.txt"); ConfigRemote = JsonConvert.DeserializeObject<DTO_Config>(Cfg); VersionR = ConfigRemote.Version; VersionL = ConfigLocal.Version; int VersionRemote = int.Parse(ConfigRemote.Version.Replace(".", "")); int VersionLocal = int.Parse(ConfigLocal.Version.Replace(".", "")); result = VersionRemote > VersionLocal; } catch { } return result; }
第二步:下載更新包
因?yàn)镃/S的軟件更新是面對(duì)所有用戶(hù),S端除了給C端提供基本的服務(wù)外,還可以給C端提供更新包。而這個(gè)S端可以是網(wǎng)絡(luò)上的一個(gè)固定地址,也可以是局域網(wǎng)內(nèi)一個(gè)公共盤(pán)。那下載更新包無(wú)非就是去訪問(wèn)服務(wù)端的文件,然后Copy下來(lái)或下載下來(lái)。下面給出訪問(wèn)網(wǎng)絡(luò)和訪問(wèn)局域網(wǎng)兩個(gè)案例:
A、訪問(wèn)遠(yuǎn)程網(wǎng)絡(luò)地址這里采用的是WebClient
public void DownLoadFile() { if (!Directory.Exists(UpdateFiles)) { Directory.CreateDirectory(UpdateFiles); } using (WebClient webClient = new WebClient()) { try { webClient.DownloadFileCompleted = new AsyncCompletedEventHandler(client_DownloadFileCompleted); webClient.DownloadProgressChanged = new DownloadProgressChangedEventHandler(client_DownloadProgressChanged); webClient.DownloadFileAsync(new Uri(CheckUpdateURL "\\UpdateFile.rar"), UpdateFiles "\\UpdateFile.rar"); } catch (WebException ex) { MessageBox.Show(ex.Message, "系統(tǒng)提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
這里面應(yīng)用到兩個(gè)方法,DownloadProgressChanged,監(jiān)聽(tīng)異步下載的進(jìn)度;DownloadFileCompleted,監(jiān)聽(tīng)完成異步文件下載;
private void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) { this.progressBarUpdate.Minimum = 0; this.progressBarUpdate.Maximum = (int)e.TotalBytesToReceive; this.progressBarUpdate.Value = (int)e.BytesReceived; this.lblPercent.Text = e.ProgressPercentage "%"; }
private void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) { if (e.Error != null) { MessageBox.Show(e.Error.Message, "系統(tǒng)提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { this.lblMessage.Text = "下載完成"; //復(fù)制更新文件替換舊文件 DirectoryInfo TheFolder = new DirectoryInfo(UpdateFiles); foreach (FileInfo NextFile in TheFolder.GetFiles()) { File.Copy(NextFile.FullName, Application.StartupPath NextFile.Name, true); } } }
B、訪問(wèn)服務(wù)端公共盤(pán),直接采用File.Copy
public void GetRemoteFile() { try { DirectoryInfo TheFolder = new DirectoryInfo(CheckUpdateURL); FileInfo[] FileList = TheFolder.GetFiles(); this.progressBarUpdate.Minimum = 0; this.progressBarUpdate.Maximum = FileList.Length; foreach (FileInfo NextFile in FileList) { if (NextFile.Name != "Config.txt") { File.Copy(NextFile.FullName, exePath "\\" NextFile.Name, true); } this.lblMessage.Text = "更新" NextFile.Name; this.progressBarUpdate.Value = 1; this.lblPercent.Text = "更新進(jìn)度... " (this.progressBarUpdate.Value / FileList.Length) * 100 "%"; } this.lblMessage.Text = "更新完成"; //更改本地版本號(hào)為最新版本號(hào) ConfigLocal.Version = VersionR; string cfgs = JsonConvert.SerializeObject(ConfigLocal); TxtWrite(Application.StartupPath "\\Config.txt", cfgs); } catch (Exception ex) { MessageBox.Show(ex.Message, "系統(tǒng)提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
第三步:替換本地文件
這一步或許在第二步中已經(jīng)實(shí)現(xiàn)了,如果你采用的是File.Copy。替換也就是復(fù)制粘貼的問(wèn)題。采用WebClient下載了zip包,那還需解壓一下壓縮包然后再File.Copy。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
用 C# 編寫(xiě)一個(gè)停放在任務(wù)欄上的圖標(biāo)程序
用 C# 編寫(xiě)一個(gè)停放在任務(wù)欄上的圖標(biāo)程序...2007-03-03C# HttpClient 如何使用 Consul 發(fā)現(xiàn)服務(wù)
這篇文章主要介紹了C# HttpClient 如何使用 Consul 發(fā)現(xiàn)服務(wù),幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2021-02-02