终端设备自动更新

一、做设备当多台的时候最好做自动更新的控件,每一次通过服务器上传和下载,下面提供一下自动更新思路。

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);
            }
        }
复制代码

 

 
复制代码

 

posted @   NangFah  阅读(42)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
点击右上角即可分享
微信分享提示