Winform使用FTP實現(xiàn)自動更新
本文實例為大家分享了Winform使用FTP實現(xiàn)自動更新的具體代碼,供大家參考,具體內(nèi)容如下
實現(xiàn)思路:在主程序打開前實現(xiàn)判斷是否需要更新(可以通過數(shù)據(jù)庫表記錄一下版本號或者別的方式記錄是否需要更新),若需要更新時從ftp站點下載更新包(關(guān)于配置ftp站點自己可以搜這里不再做詳述)。自己可以制定后綴格式的包或者別的!一般用壓縮包的形式來存放最新程序,將文件下載到本地路徑,在關(guān)閉當(dāng)前程序打開更新程序做解壓替換文件操作,或者可以用批處理文、可執(zhí)行文件來做操作都行!
1.判斷是否有新版本。
2.通過ftp將更新包下載至本地路徑。
3.打開更新程序(批處理文件或可執(zhí)行文件)同時關(guān)閉所有主程序進(jìn)程。
4.在更新程序中進(jìn)行解壓、替換操作。
5.待替換完畢刪除本地更新包(可選)。
6.打開新程序同時關(guān)閉所有更新程序進(jìn)程。
代碼:
1.在程序入口處Program.cs中做判斷:
//判斷版本號是否為數(shù)據(jù)庫表的版本號 ? if (edition == "2021.5.12.0.1")//版本號自己可以判斷 ? ? ? ? ? ? { ? ? ? ? ? ? ? ? var resulta = MessageBox.Show("有可用更新,是否更新?", "系統(tǒng)提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question); ? ? ? ? ? ? ? ? if (resulta == DialogResult.No) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? Application.Run(new Form1()); ? ? ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //從服務(wù)器獲取新壓縮文件后下載至某一路徑 ? ? ? ? ? ? ? ? UpdatateHelp help = new UpdatateHelp();//更新類 ? ? ? ? ? ? ? ? help.IP = "xxx.xx.xx.xxx"; ? ? ? ? ? ? ? ? help.ServerFile = "OldDemoUpd.zip"; ? ? ? ? ? ? ? ? help.User = "Administrator"; ? ? ? ? ? ? ? ? help.Password = "*****"; ? ? ? ? ? ? ? ? string message = string.Empty; ? ? ? ? ? ? ? ? if (!help.DownloadFile(out message)) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? var result = MessageBox.Show(message, "系統(tǒng)提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); ? ? ? ? ? ? ? ? ? ? if (result == DialogResult.Yes) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? Application.Run(new Form1()); ? ? ? ? ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? //強(qiáng)制關(guān)閉進(jìn)程 ? ? ? ? ? ? ? ? ? ? ? ? ? ? var proc = System.Diagnostics.Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName); ? ? ? ? ? ? ? ? ? ? ? ? foreach (Process item in proc) ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? item.Kill(); ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? //替換程序文件(用一個update程序負(fù)責(zé)解壓程序并替換文件,在刪除壓縮文件) ? ? ? ? ? ? ? ? System.Diagnostics.Process.Start(Application.StartupPath + "\\Update\\" + "AutoUpdate.exe"); ? ? ? ? ? ? ? ? //關(guān)閉當(dāng)前進(jìn)程 ?? ? ? ? ? ? ? ? ? foreach (Process item in System.Diagnostics.Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName)) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? item.Kill(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? }
2.更新幫助類UpdatateHelp
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; namespace OldDemo { ? ? class UpdatateHelp ? ? { ? ? ? ? /// <summary> ? ? ? ? /// 服務(wù)器IP ? ? ? ? /// </summary> ? ? ? ? public string IP { get; set; } ? ? ? ? /// <summary> ? ? ? ? /// 服務(wù)器文件和下載到本地文件名一致 ? ? ? ? /// </summary> ? ? ? ? public string ServerFile { get; set; } ? ? ? ? /// <summary> ? ? ? ? /// 服務(wù)器用戶名 ? ? ? ? /// </summary> ? ? ? ? public string User { get; set; } ? ? ? ? /// <summary> ? ? ? ? /// 服務(wù)器密碼 ? ? ? ? /// </summary> ? ? ? ? public string Password { get; set; } ? ? ? ? /// <summary> ? ? ? ? /// 下載服務(wù)器文件 ? ? ? ? /// </summary> ? ? ? ? /// <param name="Message">返回信息</param> ? ? ? ? /// <returns></returns> ? ? ? ? public bool DownloadFile(out string Message) ? ? ? ? { ? ? ? ? ? ? FtpWebRequest reqFTP; ? ? ? ? ? ? try ? ? ? ? ? ? { ? ? ? ? ? ? ? ? FileStream outputStream = new FileStream(Path.GetTempPath()+ ServerFile, FileMode.Create);//本地緩存目錄 ? ? ? ? ? ? ? ? reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + IP + "http://"+ ServerFile)); ? ? ? ? ? ? ? ? reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;//指定當(dāng)前請求是什么命令(upload,download,filelist等) ? ? ? ? ? ? ? ? reqFTP.UseBinary = true;//指定文件傳輸?shù)念愋? ? ? ? ? ? ? ? ? reqFTP.Credentials = new NetworkCredential(User, Password); //指定登錄ftp服務(wù)器的用戶名和密碼。 ? ? ? ? ? ? ? ? reqFTP.KeepAlive = false;//指定在請求完成之后是否關(guān)閉到 FTP 服務(wù)器的控制連接 ? ? ? ? ? ? ? ? //reqFTP.UsePassive = true;//指定使用主動模式還是被動模式 ? ? ? ? ? ? ? ? //reqFTP.Proxy = null;//設(shè)置不使用代理 ? ? ? ? ? ? ? ? //reqFTP.Timeout = 3000; ? ? ? ? ? ? ? ? FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); ? ? ? ? ? ? ? ? Stream ftpStream = response.GetResponseStream(); ? ? ? ? ? ? ? ? long cl = response.ContentLength; ? ? ? ? ? ? ? ? int bufferSize = 2048; ? ? ? ? ? ? ? ? int readCount; ? ? ? ? ? ? ? ? byte[] buffer = new byte[bufferSize]; ? ? ? ? ? ? ? ? readCount = ftpStream.Read(buffer, 0, bufferSize); ? ? ? ? ? ? ? ? while (readCount > 0) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? outputStream.Write(buffer, 0, readCount); ? ? ? ? ? ? ? ? ? ? readCount = ftpStream.Read(buffer, 0, bufferSize); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ftpStream.Close(); ? ? ? ? ? ? ? ? outputStream.Close(); ? ? ? ? ? ? ? ? response.Close(); ? ? ? ? ? ? ? ? Message = "下載更新文件成功!"; ? ? ? ? ? ? ? ? return true; ? ? ? ? ? ? } ? ? ? ? ? ? catch (Exception ex) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? Message = "下載更新文件失?。≡颍?+ex.Message + " 按是進(jìn)入原程序,按否關(guān)閉程序!"; ? ? ? ? ? ? ? ? return false; ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } ? ? } }
3.關(guān)閉主程序進(jìn)程打開更新程序AutoUpdate.exe,可以在Program中執(zhí)行也可以在程序中新建一個窗體顯示進(jìn)度條等!此處用Form1窗體來做解壓處理,需要注意的地方是我引用了using Ionic.Zip;可以在Nuget下搜一下DotNetZip,該dll是針對文件解壓縮幫助類,此只是例舉解壓,有興趣自己研究別的實現(xiàn)!
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Windows.Forms; using Ionic.Zip; namespace AutoUpdate { ? ? public partial class Form1 : Form ? ? { ? ? ? ? public Form1() ? ? ? ? { ? ? ? ? ? ? InitializeComponent(); ? ? ? ? ? } ? ? ? ? private void Form1_Load(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? StartUpdate(); ? ? ? ? } ? ? ? ? void StartUpdate() ? ? ? ? { ? ? ? ? ? ? string FileLoad = System.IO.Path.GetTempPath() + "OldDemoUpd.zip"; ? ? ? ? ? ? string IndexLoad = Application.StartupPath + "\\"; ? ? ? ? ? ? var lis = IndexLoad.Split('\\').ToList(); ? ? ? ? ? ? lis.Remove(""); ? ? ? ? ? ? lis.Remove("Update");//由于更新程序沒和主程序目錄同步,所以需要返回到Update文件夾上一級主程序目錄中。 ? ? ? ? ? ? IndexLoad = string.Join("\\",lis); ? ? ? ? ? ? //1.解壓臨時文件包到當(dāng)前路徑并刪除壓縮包 ? ? ? ? ? ? if (System.IO.File.Exists(FileLoad)) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? label1.Text = "正在解壓軟件更新包..."; ? ? ? ? ? ? ? ? //存在就解壓 ? ? ? ? ? ? ? ? if (!Decompression(FileLoad, IndexLoad, true)) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? MessageBox.Show("解壓更新包失敗,請重試!", "系統(tǒng)提示"); ? ? ? ? ? ? ? ? ? ? //關(guān)閉當(dāng)前進(jìn)程 ?? ? ? ? ? ? ? ? ? ? ? foreach (System.Diagnostics.Process item in System.Diagnostics.Process.GetProcessesByName(System.Diagnostics.Process.GetCurrentProcess().ProcessName)) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? item.Kill(); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? label1.Text = "正在刪除軟件更新包..."; ? ? ? ? ? ? ? ? //刪除壓縮包 ? ? ? ? ? ? ? ? System.IO.File.Delete(FileLoad); ? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? { ? ? ? ? ? ? ? ? MessageBox.Show("軟件更新包不存在,請重新打開程序以獲取更新包!", "系統(tǒng)提示"); ? ? ? ? ? ? ? ? //關(guān)閉當(dāng)前進(jìn)程 ?? ? ? ? ? ? ? ? ? foreach (System.Diagnostics.Process item in System.Diagnostics.Process.GetProcessesByName(System.Diagnostics.Process.GetCurrentProcess().ProcessName)) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? item.Kill(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? label1.Text = "更新成功,請稍后..."; ? ? ? ? ? ? //2.打開更新后程序 ? ? ? ? ? ? System.Diagnostics.Process.Start(IndexLoad + "\\" + "OldDemo.exe"); ? ? ? ? ? ? //關(guān)閉當(dāng)前進(jìn)程 ?? ? ? ? ? ? ? foreach (System.Diagnostics.Process item in System.Diagnostics.Process.GetProcessesByName(System.Diagnostics.Process.GetCurrentProcess().ProcessName)) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? item.Kill(); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? /// <summary> ? ? ? ? /// 解壓ZIP文件 ? ? ? ? /// </summary> ? ? ? ? /// <param name="strZipPath">待解壓的ZIP文件</param> ? ? ? ? /// <param name="strUnZipPath">解壓的目錄</param> ? ? ? ? /// <param name="overWrite">是否覆蓋</param> ? ? ? ? /// <returns>成功:true/失?。篺alse</returns> ? ? ? ? public static bool Decompression(string strZipPath, string strUnZipPath, bool overWrite) ? ? ? ? { ? ? ? ? ? ? try ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ReadOptions options = new ReadOptions(); ? ? ? ? ? ? ? ? options.Encoding = Encoding.Default;//設(shè)置編碼,解決解壓文件時中文亂碼 ? ? ? ? ? ? ? ? using (ZipFile zip = ZipFile.Read(strZipPath, options)) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? foreach (ZipEntry entry in zip) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? if (string.IsNullOrEmpty(strUnZipPath)) ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? strUnZipPath = strZipPath.Split('.').First(); ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? if (overWrite) ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? entry.Extract(strUnZipPath, ExtractExistingFileAction.OverwriteSilently);//解壓文件,如果已存在就覆蓋 ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? entry.Extract(strUnZipPath, ExtractExistingFileAction.DoNotOverwrite);//解壓文件,如果已存在不覆蓋 ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? return true; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? catch (Exception) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? return false; ? ? ? ? ? ? } ? ? ? ? } ? ? } }
4.需要注意的幾個地方有:
4.1在主程序的生成目錄下創(chuàng)建一個文件夾Update;
4.2把更新程序的生成文件放入Update文件夾下邊,主要是主程序Program中這一段(主程序目錄和更新目錄不是同級):
//替換程序文件(用一個update程序負(fù)責(zé)解壓程序并替換文件,在刪除壓縮文件) ?System.Diagnostics.Process.Start(Application.StartupPath + "\\Update\\" + "AutoUpdate.exe");
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C# ping網(wǎng)絡(luò)IP 實現(xiàn)網(wǎng)絡(luò)狀態(tài)檢測的方法
下面小編就為大家?guī)硪黄狢# ping網(wǎng)絡(luò)IP 實現(xiàn)網(wǎng)絡(luò)狀態(tài)檢測的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-08-08C#通過標(biāo)簽軟件Bartender的ZPL命令打印條碼
這篇文章介紹了C#通過標(biāo)簽軟件Bartender的ZPL命令打印條碼,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01C# DataSet查看返回結(jié)果集的實現(xiàn)
這篇文章主要介紹了C# DataSet查看返回結(jié)果集的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10C#實現(xiàn)的三種模擬自動登錄和提交POST信息的方法
這篇文章主要介紹了C#實現(xiàn)的三種模擬自動登錄和提交POST信息的方法,分別列舉了WebBrowser、WebClient及HttpWebRequest實現(xiàn)自動登錄及提交POST的相關(guān)實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11