C# WinForm實(shí)現(xiàn)自動更新程序之客戶端的示例代碼
前言
第二步理論上我們該寫客戶端了,但是,在此之前,需要先介紹下一些必要的方法以及操作。
寫代碼還是要盡量的保證通用性,以便以后需要的時候可以拿來稍微改改甚至直接使用。所以在這里我們將自動更新的程序抽象出來,即對于客戶端來說,它只包含三個文件(AutoUpdate.dll、AutoUpdate.exe、UpdateList.xml,如果是.NET Framework的話,其實(shí)是沒有AutoUpdate.dll文件的,就一個exe就足夠了。這也是我為什么一直不用NET Core來寫Winform程序的原因之一);然后將這三個文件放到主程序的目錄中即可。
然后就是傳參調(diào)用,在Program文件中做了以下代碼操作。所以調(diào)用的時候需要將主程序的執(zhí)行目錄以及進(jìn)程名傳過來,作用分別是再更新完后自動啟動以及更新之前把相關(guān)的進(jìn)程殺掉以便完成更新。
同時可以看到在更新的時候,有一個圖片旋轉(zhuǎn)的動作,也一并放到此篇文章中。
開發(fā)環(huán)境
.NET Core 3.1
開發(fā)工具
Visual Studio 2019
實(shí)現(xiàn)代碼
//更新程序 namespace AutoUpdate { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main(string[] args) { if(args.Length != 1) { return; } var arg = args[0].Split("|*|"); if(arg.Length == 0) { return; } string runPath = arg[0]; string procesName = arg[1]; Process[] processes = Process.GetProcesses(); foreach(Process process in processes) { if(process.ProcessName == procesName) { process.Kill(true); } } Application.SetHighDpiMode(HighDpiMode.SystemAware); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form_update(runPath)); } } }
//主程序 namespace AutoUpdate.Test { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Update(); Application.SetHighDpiMode(HighDpiMode.SystemAware); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } readonly static string updateXml = Application.StartupPath + "UpdateList.xml"; /// <summary> /// 獲取本地更新地址 /// </summary> /// <returns></returns> static string GetUpdateUrl() { XElement xele = XElement.Load(updateXml); string url = xele.Element("url").Value; return url; } /// <summary> /// 獲取本地更新文件 /// </summary> /// <returns></returns> static string GetUpdateFiles() { XDocument xdoc = XDocument.Load(updateXml); var files = from f in xdoc.Root.Element("files").Elements() select new { name = f.Attribute("name").Value, version = f.Attribute("version").Value }; return JsonConvert.SerializeObject(files); } /// <summary> /// 檢查是否需要更新 /// </summary> /// <returns></returns> static bool CheckUpdate() { string url = GetUpdateUrl(); HttpResult httpResult = HttpUtil.HttpRequest(new HttpItem(url + "GetUpdateFiles", requestData: GetUpdateFiles())); if(httpResult.Status) { UpdateModel_Out output = JsonConvert.DeserializeObject<UpdateModel_Out>(httpResult.HttpStringData); if(output.updateList.Count > 0) return true; } return false; } static void Update() { if(CheckUpdate()) { string processName = Assembly.GetExecutingAssembly().GetName().Name; ProcessStartInfo info = new ProcessStartInfo(Application.StartupPath + "AutoUpdate.exe", Process.GetCurrentProcess().MainModule.FileName + "|*|" + processName); Process.Start(info); Environment.Exit(0); } } } }
public static class ImageEx { /// <summary> /// 旋轉(zhuǎn)圖片 /// </summary> /// <param name="image"></param> /// <param name="angle"></param> /// <returns></returns> public static Image RotateImage(this Image image, float angle) { if(image == null) throw new ArgumentNullException("image"); float dx = image.Width / 2.0f; float dy = image.Height / 2.0f; Bitmap rotatedBmp = new Bitmap(image.Width, image.Height); rotatedBmp.SetResolution(image.HorizontalResolution, image.VerticalResolution); Graphics g = Graphics.FromImage(rotatedBmp); g.TranslateTransform(dx, dy); g.RotateTransform(angle); g.TranslateTransform(-dx, -dy); g.DrawImage(image, new PointF(0, 0)); g.Dispose(); return rotatedBmp; } }
實(shí)現(xiàn)效果
代碼解析
這里可以關(guān)注下在主程序中的獲取更新地址以及文件等方法,其實(shí)我這里是有重復(fù)判斷的,即在主程序中判斷了一遍,還會在更新程序中判斷一遍,如果覺得不需要,可以執(zhí)行選擇去掉,全部交給更新程序去做。但是也就需要統(tǒng)一放在更新程序的入口中做處理了,相對而言,我覺得寫兩遍還是很方便。
到此這篇關(guān)于C# WinForm實(shí)現(xiàn)自動更新程序之客戶端的示例代碼的文章就介紹到這了,更多相關(guān)C# WinForm更新程序內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
WinForm中DataGridView添加,刪除,修改操作具體方法
這篇文章介紹了WinForm中DataGridView添加,刪除,修改操作具體方法,有需要的朋友可以參考一下2013-10-10基于C#實(shí)現(xiàn)自定義計(jì)算的Excel數(shù)據(jù)透視表
數(shù)據(jù)透視表(Pivot?Table)是一種數(shù)據(jù)分析工具,通常用于對大量數(shù)據(jù)進(jìn)行匯總、分析和展示,本文主要介紹了C#實(shí)現(xiàn)自定義計(jì)算的Excel數(shù)據(jù)透視表的相關(guān)知識,感興趣的可以了解下2023-12-12C#利用原圖和水印圖的重疊簡單實(shí)現(xiàn)水印的方法
這篇文章主要介紹了C#利用原圖和水印圖的重疊簡單實(shí)現(xiàn)水印的方法,實(shí)例演示了完整的水印操作類實(shí)現(xiàn)方法,需要的朋友可以參考下2016-04-04