LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發文檔 其他文檔  
 
網站管理員

WinForm 自動更新程序(二)

admin
2023年2月27日 10:15 本文熱度 1241

     第二步理論上我們該寫客戶端了,但是,在此之前,需要先介紹下一些必要的方法以及操作。

寫代碼還是要盡量的保證通用性,以便以后需要的時候可以拿來稍微改改甚至直接使用。所以在這里我們將自動更新的程序抽象出來,即對于客戶端來說,它只包含三個文件(Autoupdate.dll、Autoupdate.exe、updateList.xml,如果是.NET Framework的話,其實是沒有Autoupdate.dll文件的,就一個exe就足夠了。這也是我為什么一直不用NET Core來寫Winform程序的原因之一);然后將這三個文件放到主程序的目錄中即可。

然后就是傳參調用,在Program文件中做了以下代碼操作。所以調用的時候需要將主程序的執行目錄以及進程名傳過來,作用分別是再更新完后自動啟動以及更新之前把相關的進程殺掉以便完成更新。

同時可以看到在更新的時候,有一個圖片旋轉的動作,也一并放到此篇文章中。


開發環境:.NET Core 3.1

開發工具: Visual Studio 2019

實現代碼:

//更新程序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> /// 旋轉圖片 /// </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; } }

實現效果:


代碼解析:這里可以關注下在主程序中的獲取更新地址以及文件等方法,其實我這里是有重復判斷的,即在主程序中判斷了一遍,還會在更新程序中判斷一遍,如果覺得不需要,可以執行選擇去掉,全部交給更新程序去做。但是也就需要統一放在更新程序的入口中做處理了,相對而言,我覺得寫兩遍還是很方便。


該文章在 2023/2/27 10:15:34 編輯過
關鍵字查詢
相關文章
正在查詢...
點晴ERP是一款針對中小制造業的專業生產管理軟件系統,系統成熟度和易用性得到了國內大量中小企業的青睞。
點晴PMS碼頭管理系統主要針對港口碼頭集裝箱與散貨日常運作、調度、堆場、車隊、財務費用、相關報表等業務管理,結合碼頭的業務特點,圍繞調度、堆場作業而開發的。集技術的先進性、管理的有效性于一體,是物流碼頭及其他港口類企業的高效ERP管理信息系統。
點晴WMS倉儲管理系統提供了貨物產品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質期管理,貨位管理,庫位管理,生產管理,WMS管理系統,標簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務都免費,不限功能、不限時間、不限用戶的免費OA協同辦公管理系統。
Copyright 2010-2025 ClickSun All Rights Reserved

黄频国产免费高清视频,久久不卡精品中文字幕一区,激情五月天AV电影在线观看,欧美国产韩国日本一区二区
亚洲中文字幕精品一区二区三区 | 亚洲人AV在线播放不卡 | 亚洲欧美日韩偷拍综合一区 | 日本一区二区久久 | 亚洲国产精品综合久久网各 | 中文字幕欧美亚日 |