一、做设备当多台的时候最好做自动更新的控件,每一次通过服务器上传和下载,下面提供一下自动更新思路。
1、提供一个检测是否需要自动更新的接口,传入版本和设备类型,检测是否需要更新。
2、提供一个下载安装包的接口,传入要下载的版本号和设备类型,post下载。下载完成后安装启动即可。
///======================【流程】========================= //1、判断是否需要更新 /// <summary>判断是否需要更新</summary> /// <param name="statusCode">状态码</param> /// <param name="newVersion">新的版本号</param> /// <param name="size">安装包大小</param> /// <returns>是否需要更新</returns> public bool isNeedUpdate(out string statusCode, out string newVersion, out string nowVersion, out string exe, out string stepFile) { try { exe = ""; nowVersion = ""; newVersion = ""; stepFile = ""; statusCode = ""; filePath = ""; urlfile = ""; string reslut = ""; iniFile = new IniFile(FTP.getBasePath() + "\\ini\\Version.ini"); iniFile2 = new IniFile(FTP.getBasePath() + "\\Config.ini"); Log.WriteLogJson("isNeedUpdate", "开始"); string url = iniFile.IniReadValue("USER", "ip") + "/autoUpdate/api/autoUpdateCheck/isLatestVersion"; string equipmentNo = iniFile2.IniReadValue("SET", "DeviceId").Trim(); nowVersion = iniFile.IniReadValue("USER", "versionNo"); exe = FTP.getBasePath() + iniFile.IniReadValue("USER", "name"); Log.WriteLogJson("isNeedUpdate", equipmentNo); Dictionary<String, String> jobFailId = new Dictionary<string, string>(); jobFailId.Add("apkClass", iniFile.IniReadValue("USER", "appType")); jobFailId.Add("build", iniFile.IniReadValue("USER", "versionNo")); jobFailId.Add("equipmentNo", equipmentNo); url = getUrl(url, jobFailId); Log.WriteLogJson("isNeedUpdate", url); HttpWebRequest wbRequest = (HttpWebRequest)WebRequest.Create(url); wbRequest.Proxy = null; wbRequest.Method = "GET"; HttpWebResponse wbResponse = (HttpWebResponse)wbRequest.GetResponse(); using (Stream responseStream = wbResponse.GetResponseStream()) { using (StreamReader sReader = new StreamReader(responseStream)) { reslut = sReader.ReadToEnd(); Log.WriteLogJson("isNeedUpdate", reslut); } } if(string.IsNullOrEmpty(reslut)) { return false; } if(reslut.Contains("暂无最新版本更新")) { return false; } //解析 var mJson = JsonConvert.DeserializeObject<isLatestVersionRq>(reslut); if(mJson.code!=0) { return false; } statusCode= mJson.code.ToString(); newVersion = mJson.data.build; urlfile = mJson.data.url.ToString(); string appType2 = iniFile.IniReadValue("USER", "appType").Trim();//终端类型 filePath = FTP.getBasePath() + "SCQS_" + newVersion + ".exe"; stepFile = filePath; string size = mJson.data.apkSize.ToString(); fileSize = ulong.Parse(size); build = newVersion; nowVersion = iniFile.IniReadValue("USER", "versionNo").ToString(); Log.WriteLogJson("isNeedUpdate","最新版本号:"+ newVersion); if (Convert.ToInt32(nowVersion) < Convert.ToInt32(newVersion)) { Log.WriteLogJson("isNeedUpdate", "需要更新" ); return true; } else return false; } catch(Exception ex) { throw new Exception(ex.Message); } }
/// <summary>2、下载安装包</summary> public void downloadPack() { try { int all_count = 0; sc.start(); process = 0; System.String result = System.String.Empty; HttpWebRequest Myrq = WebRequest.Create(urlfile) as HttpWebRequest; HttpWebResponse myrp = Myrq.GetResponse() as HttpWebResponse; System.IO.Stream st = myrp.GetResponseStream(); System.IO.Stream so = new System.IO.FileStream(filePath, System.IO.FileMode.Create); System.Byte[] by = new System.Byte[1024 * 10]; System.Int32 osize = st.Read(by, 0, (System.Int32)by.Length); process = 0.2f; while (osize > 0) { so.Write(by, 0, osize); all_count += osize; sc.setSize(all_count); writeCount += (ulong)osize; process = (float)writeCount / (float)fileSize; osize = st.Read(by, 0, (System.Int32)by.Length); } so.Close(); st.Close(); myrp.Close(); Myrq.Abort(); isFinish = true; sc.stop(); } catch (Exception ex) { throw new Exception(ex.Message); } }
标签:string,iniFile,System,终端设备,USER,自动更新,new,IniReadValue From: https://www.cnblogs.com/NangFah/p/17526103.html