溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C#怎么實(shí)現(xiàn)自動(dòng)更新本地程序

發(fā)布時(shí)間:2020-09-14 15:09:30 來源:億速云 閱讀:668 作者:小新 欄目:編程語言

這篇文章將為大家詳細(xì)講解有關(guān)C#怎么實(shí)現(xiàn)自動(dòng)更新本地程序,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

關(guān)于系統(tǒng)的自動(dòng)更新。近日有一情況是需要將java端后臺(tái)最新版本的系統(tǒng)文件覆蓋本地客戶端,簡稱自動(dòng)更新了。

本地會(huì)獲取當(dāng)前系統(tǒng)的版本號(hào)去請(qǐng)求后臺(tái)java的接口數(shù)據(jù)。返回給我的是后臺(tái)壓縮包轉(zhuǎn)的base64字節(jié)流。

客戶端拿到新版本需要更新本地程序。

    if (UpdateSystem(Path.Combine(Application.StartupPath, "Version.txt"), Path.Combine(Application.StartupPath, "u.zip")))
            {
                Application.Exit();
            }
/// <summary>
        /// 讀取本地版本請(qǐng)求更新
        /// </summary>
        /// <param name="document">讀取的文件信息</param>
        /// <param name="zipPath">返回zip包本地路徑</param>
        /// <returns></returns>
        private bool UpdateSystem(string document, string zipPath)
        {
            try
            {
                Dictionary<string, string> postDic = new Dictionary<string, string>();
                //獲取文件內(nèi)的版本號(hào)
                if(File.Exists(document))
                {
                    postDic.Add("version", File.ReadAllText(document).Trim());
                }
                else
                {
                    postDic.Add("version", "0");
                }

                string postJson = JsonConvert.SerializeObject(postDic);
                string url = GetAppSettingValue("serverUrl") + "parkClient/parkClientUpdate";
                //返回的json數(shù)據(jù)
                JObject obj = (JObject)JsonConvert.DeserializeObject(PostData(postJson, url));
                string newVersion = obj["version"].ToString();
                if (!String.IsNullOrWhiteSpace(newVersion))
                {
                    byte[] bytesFile = Convert.FromBase64String(obj["byteArray"].ToString());
                    if (obj["clientMD5"].ToString() == BitConverter.ToString(
                        new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(bytesFile)).Replace("-", ""))
                    {
                        ZipCoverage(bytesFile, zipPath);

                        File.WriteAllText(document, newVersion);
                       
                    }
                }

                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
        }

        /// <summary>
        /// 解壓zip包覆蓋更新
        /// </summary>
        /// <param name="bytes">接受更新包的字節(jié)信息</param>
        /// <param name="zpath">覆蓋的路徑</param>
        private void ZipCoverage(byte[] bytes, string zpath)
        {
            File.WriteAllBytes(zpath, bytes);
            using (ZipArchive archive = ZipFile.OpenRead(zpath))
            {
                string file = null;
                foreach (ZipArchiveEntry entry in archive.Entries)
                {
                    if (!entry.FullName.EndsWith("/"))
                    {
                        file = Path.Combine(Application.StartupPath, entry.FullName);
                        if (File.Exists(file))
                        {
                            File.Delete(file);
                        }
                    }
                }
            }
            ZipFile.ExtractToDirectory(zpath, Application.StartupPath);
           
        }

        /// <summary>
        /// 獲取配置文件中的appSettings節(jié)中的配置內(nèi)容
        /// </summary>
        /// <param name="appSettingKey"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        private string GetAppSettingValue(string appSettingKey)
        {
            ExeConfigurationFileMap map = new ExeConfigurationFileMap { ExeConfigFilename = @"TDH.Parking.Client.exe.config" };
            return ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None).AppSettings.Settings[appSettingKey].Value;
        }
byte[] bytesFile = Convert.FromBase64String(obj["byteArray"].ToString());

這里是拿到的字節(jié)流了。

C#怎么實(shí)現(xiàn)自動(dòng)更新本地程序

C#怎么實(shí)現(xiàn)自動(dòng)更新本地程序

這個(gè)方法可以解決在同一個(gè)解決方案中有多個(gè)項(xiàng)目可以讀取到同一個(gè)項(xiàng)目下的App.config文件。

注意:其中有引用到的類庫,C#怎么實(shí)現(xiàn)自動(dòng)更新本地程序 這是是用來操作壓縮包的。

說下思路:第一步其實(shí)就是拿到壓縮包的字節(jié)流再保存到本地,第二步就是循環(huán)讀取壓縮包的文件替換本地的文件,完成本地系統(tǒng)的版本更新。

無論簡單與復(fù)雜,都需一步步向前方邁進(jìn)。

關(guān)于C#怎么實(shí)現(xiàn)自動(dòng)更新本地程序就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI