C# Winform 自動更新程序?qū)嵗斀?/h1>
來源:本站原創(chuàng)|時間:2020-01-10|欄目:C#教程|點擊: 次
本文實例為大家分享了C# Winform 自動更新程序,供大家參考,具體內(nèi)容如下
第一步:檢查更新
檢查更新其實無非就是去比較更新包的版本和本地軟件版本,如果高則更新、低則不更新。怎么獲取版本號方法很多,本案例是獲取軟件的配置文件。
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;
}
第二步:下載更新包
因為C/S的軟件更新是面對所有用戶,S端除了給C端提供基本的服務外,還可以給C端提供更新包。而這個S端可以是網(wǎng)絡上的一個固定地址,也可以是局域網(wǎng)內(nèi)一個公共盤。那下載更新包無非就是去訪問服務端的文件,然后Copy下來或下載下來。下面給出訪問網(wǎng)絡和訪問局域網(wǎng)兩個案例:
A、訪問遠程網(wǎng)絡地址這里采用的是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);
}
}
}
這里面應用到兩個方法,DownloadProgressChanged,監(jiān)聽異步下載的進度;DownloadFileCompleted,監(jiān)聽完成異步文件下載;
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 = "下載完成";
//復制更新文件替換舊文件
DirectoryInfo TheFolder = new DirectoryInfo(UpdateFiles);
foreach (FileInfo NextFile in TheFolder.GetFiles())
{
File.Copy(NextFile.FullName, Application.StartupPath NextFile.Name, true);
}
}
}
B、訪問服務端公共盤,直接采用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 = "更新進度... " (this.progressBarUpdate.Value / FileList.Length) * 100 "%";
}
this.lblMessage.Text = "更新完成";
//更改本地版本號為最新版本號
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)實現(xiàn)了,如果你采用的是File.Copy。替換也就是復制粘貼的問題。采用WebClient下載了zip包,那還需解壓一下壓縮包然后再File.Copy。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持我們。
本文實例為大家分享了C# Winform 自動更新程序,供大家參考,具體內(nèi)容如下
第一步:檢查更新
檢查更新其實無非就是去比較更新包的版本和本地軟件版本,如果高則更新、低則不更新。怎么獲取版本號方法很多,本案例是獲取軟件的配置文件。
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; }
第二步:下載更新包
因為C/S的軟件更新是面對所有用戶,S端除了給C端提供基本的服務外,還可以給C端提供更新包。而這個S端可以是網(wǎng)絡上的一個固定地址,也可以是局域網(wǎng)內(nèi)一個公共盤。那下載更新包無非就是去訪問服務端的文件,然后Copy下來或下載下來。下面給出訪問網(wǎng)絡和訪問局域網(wǎng)兩個案例:
A、訪問遠程網(wǎng)絡地址這里采用的是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); } } }
這里面應用到兩個方法,DownloadProgressChanged,監(jiān)聽異步下載的進度;DownloadFileCompleted,監(jiān)聽完成異步文件下載;
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 = "下載完成"; //復制更新文件替換舊文件 DirectoryInfo TheFolder = new DirectoryInfo(UpdateFiles); foreach (FileInfo NextFile in TheFolder.GetFiles()) { File.Copy(NextFile.FullName, Application.StartupPath NextFile.Name, true); } } }
B、訪問服務端公共盤,直接采用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 = "更新進度... " (this.progressBarUpdate.Value / FileList.Length) * 100 "%"; } this.lblMessage.Text = "更新完成"; //更改本地版本號為最新版本號 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)實現(xiàn)了,如果你采用的是File.Copy。替換也就是復制粘貼的問題。采用WebClient下載了zip包,那還需解壓一下壓縮包然后再File.Copy。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持我們。
您可能感興趣的文章
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新聞效果的方法
- 01-10C#實現(xiàn)Winform中打開網(wǎng)頁頁面的方法
- 01-10winform實現(xiàn)創(chuàng)建最前端窗體的方法
- 01-10WinForm實現(xiàn)自定義右下角提示效果的方法
- 01-10.net2.0+ Winform項目實現(xiàn)彈出容器層
- 01-10winform 實現(xiàn)控制輸入法
- 01-10WinForm實現(xiàn)程序一段時間不運行自動關(guān)閉的方法
- 01-10Winform消除button按下出現(xiàn)的虛線簡單實現(xiàn)方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當前操作系統(tǒng)已
隨機閱讀
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10C#中split用法實例總結(jié)
- 04-02jquery與jsp,用jquery
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10delphi制作wav文件的方法
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10SublimeText編譯C開發(fā)環(huán)境設置