最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C# winform實(shí)現(xiàn)自動(dòng)更新

 更新時(shí)間:2024年10月29日 08:38:04   作者:劉向榮  
這篇文章主要為大家詳細(xì)介紹了C# winform實(shí)現(xiàn)自動(dòng)更新的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

1.檢查當(dāng)前的程序和服務(wù)器的最新程序的版本,如果低于服務(wù)端的那么才能升級(jí)

2.服務(wù)端的文件打包.zip文件

3.把壓縮包文件解壓縮并替換客戶(hù)端的debug下所有文件。

4.創(chuàng)建另外的程序?yàn)榱私鈮嚎s覆蓋掉原始的低版本的客戶(hù)程序。

有個(gè)項(xiàng)目Update 負(fù)責(zé)在應(yīng)該關(guān)閉之后復(fù)制解壓文件夾 完成更新

這里選擇winform項(xiàng)目,項(xiàng)目名Update

以下是 Update/Program.cs 文件的內(nèi)容:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Threading;
using System.Windows.Forms;

namespace Update
{
    internal static class Program
    {
        private static readonly HashSet<string> selfFiles = new HashSet<string> { "Update.pdb", "Update.exe", "Update.exe.config" };

        [STAThread]
        static void Main()
        {
            string delay = ConfigurationManager.AppSettings["delay"];

            Thread.Sleep(int.Parse(delay));

            string exePath = null;
            string path = AppDomain.CurrentDomain.BaseDirectory;
            string zipfile = Path.Combine(path, "Update.zip");

            try
            {
                using (ZipArchive archive = ZipFile.OpenRead(zipfile))
                {
                    foreach (ZipArchiveEntry entry in archive.Entries)
                    {
                        if (selfFiles.Contains(entry.FullName))
                        {
                            continue;
                        }

                        string filepath = Path.Combine(path, entry.FullName);

                        if (filepath.EndsWith(".exe"))
                        {
                            exePath = filepath;
                        }
                        entry.ExtractToFile(filepath, true);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("升級(jí)失敗" + ex.Message);
                throw;
            }

            if (File.Exists(zipfile))
                File.Delete(zipfile);

            if (exePath == null)
            {
                MessageBox.Show("找不到可執(zhí)行文件!");
                return;
            }

            Process process = new Process();
            process.StartInfo = new ProcessStartInfo(exePath);
            process.Start();
        }

    }
}

以下是 App.config 文件的內(nèi)容:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
    </startup>
	<appSettings>
		<add key="delay" value="3000"/>
	</appSettings>
</configuration>

winform應(yīng)用

軟件版本

[assembly: AssemblyFileVersion("1.0.0.0")]

if (JudgeUpdate())
{
    UpdateApp();
}

檢查更新

private bool JudgeUpdate()
{
    string url = "http://localhost:8275/api/GetVersion";

    string latestVersion = null;
    try
    {
        using (HttpClient client = new HttpClient())
        {
            Task<HttpResponseMessage> httpResponseMessage = client.GetAsync(url);
            httpResponseMessage.Wait();

            HttpResponseMessage response = httpResponseMessage.Result;
            if (response.IsSuccessStatusCode)
            {
                Task<string> strings = response.Content.ReadAsStringAsync();
                strings.Wait();

                JObject jObject = JObject.Parse(strings.Result);
                latestVersion = jObject["version"].ToString();

            }
        }

        if (latestVersion != null)
        {
            var versioninfo = FileVersionInfo.GetVersionInfo(Application.ExecutablePath);
            if (Version.Parse(latestVersion) > Version.Parse(versioninfo.FileVersion))
            {
                return true;
            }
        }
    }
    catch (Exception)
    {
        throw;
    }
    return false;
}

執(zhí)行更新

public void UpdateApp()
{
    string url = "http://localhost:8275/api/GetZips";
    string zipName = "Update.zip";
    string updateExeName = "Update.exe";

    using (HttpClient client = new HttpClient())
    {
        Task<HttpResponseMessage> httpResponseMessage = client.GetAsync(url);
        httpResponseMessage.Wait();

        HttpResponseMessage response = httpResponseMessage.Result;
        if (response.IsSuccessStatusCode)
        {
            Task<byte[]> bytes = response.Content.ReadAsByteArrayAsync();
            bytes.Wait();

            string path = AppDomain.CurrentDomain.BaseDirectory + "/" + zipName;

            using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
            {
                fs.Write(bytes.Result, 0, bytes.Result.Length);
            }
        }

        Process process = new Process() { StartInfo = new ProcessStartInfo(updateExeName) };
        process.Start();
        Environment.Exit(0);
    }
}

服務(wù)端

using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using System.IO.Compression;

namespace WebApplication1.Controllers
{
    [ApiController]
    [Route("api")]
    public class ClientUpdateController : ControllerBase
    {

        private readonly ILogger<ClientUpdateController> _logger;

        public ClientUpdateController(ILogger<ClientUpdateController> logger)
        {
            _logger = logger;
        }

        /// <summary>
        /// 獲取版本號(hào)
        /// </summary>
        /// <returns>更新版本號(hào)</returns>
        [HttpGet]
        [Route("GetVersion")]
        public IActionResult GetVersion()
        {
            string? res = null;
            string zipfile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot", "UpdateZip", "Update.zip");
            string exeName = null;

            using (ZipArchive archive = ZipFile.OpenRead(zipfile))
            {
                foreach (ZipArchiveEntry entry in archive.Entries)
                {
                    //string filepath = Path.Combine(path, entry.FullName);

                    if (entry.FullName.EndsWith(".exe") && !entry.FullName.Equals("Update.exe"))
                    {
                        entry.ExtractToFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot", "UpdateZip", entry.FullName), true);
                        exeName = entry.FullName;
                    }
                }
            }

            FileVersionInfo versioninfo = FileVersionInfo.GetVersionInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot", "UpdateZip", exeName));
            res = versioninfo.FileVersion;

            return Ok(new { version = res?.ToString() });
        }

        /// <summary>
        /// 獲取下載地址
        /// </summary>
        /// <returns>下載地址</returns>
        [HttpGet]
        [Route("GetUrl")]
        public IActionResult GetUrl()
        {
            // var $"10.28.75.159:{PublicConfig.ServicePort}"
            return Ok();
        }


        /// <summary>
        /// 獲取下載的Zip壓縮包
        /// </summary>
        /// <returns>下載的Zip壓縮包</returns>
        [HttpGet]
        [Route("GetZips")]
        public async Task<IActionResult> GetZips()
        {
            // 創(chuàng)建一個(gè)內(nèi)存流來(lái)存儲(chǔ)壓縮文件
            using (var memoryStream = new MemoryStream())
            {
                // 構(gòu)建 ZIP 文件的完整路徑
                var zipFilePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "UpdateZip", "Update.zip");
                // 檢查文件是否存在
                if (!System.IO.File.Exists(zipFilePath))
                {
                    return NotFound("The requested ZIP file does not exist.");
                }
                // 讀取文件內(nèi)容
                var fileBytes = System.IO.File.ReadAllBytes(zipFilePath);
                // 返回文件
                return File(fileBytes, "application/zip", "Update.zip");
            }
        }

    }
}

到此這篇關(guān)于C# winform實(shí)現(xiàn)自動(dòng)更新的文章就介紹到這了,更多相關(guān)winform自動(dòng)更新內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • [C#].NET中幾種Timer的使用實(shí)例

    [C#].NET中幾種Timer的使用實(shí)例

    本篇文章主要介紹了.NET中幾種Timer的使用,具有一定的參考價(jià)值,有興趣的可以了解一下。
    2016-12-12
  • 在C#中捕獲內(nèi)存不足異常

    在C#中捕獲內(nèi)存不足異常

    這篇文章主要介紹了在C#中捕獲內(nèi)存不足異常,下面文章內(nèi)容圍繞如何在C#中捕獲內(nèi)存不足異常的相關(guān)資料展開(kāi)詳細(xì)內(nèi)容,具有一定的參考價(jià)值,需要的小伙伴可以參考一下,希望對(duì)你有所幫助
    2021-12-12
  • C#中阻止硬件休眠的多種實(shí)現(xiàn)方法

    C#中阻止硬件休眠的多種實(shí)現(xiàn)方法

    本文介紹了在C#中阻止系統(tǒng)休眠的多種方法,核心方案是通過(guò)調(diào)用Windows API的SetThreadExecutionState函數(shù),可控制顯示器關(guān)閉和系統(tǒng)休眠行為,感興趣的可以了解一下
    2025-07-07
  • C#結(jié)合Spire.Doc?for?.NET實(shí)現(xiàn)將XML轉(zhuǎn)為PDF

    C#結(jié)合Spire.Doc?for?.NET實(shí)現(xiàn)將XML轉(zhuǎn)為PDF

    可擴(kuò)展標(biāo)記語(yǔ)言(XML)文件是一種標(biāo)準(zhǔn)的文本文件,它使用自定義標(biāo)簽來(lái)描述文檔的結(jié)構(gòu)及其他特性,本文將演示如何使用?Spire.Doc?for?.NET?在?C#?和?VB.NET?中實(shí)現(xiàn)?XML?到?PDF?的轉(zhuǎn)換,有需要的可以了解下
    2026-03-03
  • C# 設(shè)計(jì)模式系列教程-橋接模式

    C# 設(shè)計(jì)模式系列教程-橋接模式

    橋接模式降低了沿著兩個(gè)或多個(gè)維度擴(kuò)展時(shí)的復(fù)雜度,防止類(lèi)的過(guò)度膨脹,解除了兩個(gè)或多個(gè)維度之間的耦合,使它們沿著各自方向變化而不互相影響。
    2016-06-06
  • UnityShader使用速度映射圖實(shí)現(xiàn)運(yùn)動(dòng)模糊

    UnityShader使用速度映射圖實(shí)現(xiàn)運(yùn)動(dòng)模糊

    這篇文章主要為大家詳細(xì)介紹了UnityShader使用速度映射圖實(shí)現(xiàn)運(yùn)動(dòng)模糊,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-02-02
  • c# 數(shù)據(jù)類(lèi)型占用的字節(jié)數(shù)介紹

    c# 數(shù)據(jù)類(lèi)型占用的字節(jié)數(shù)介紹

    本篇文章主要是對(duì)c#中數(shù)據(jù)類(lèi)型占用的字節(jié)數(shù)進(jìn)行了詳細(xì)的介紹。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助
    2014-01-01
  • C#實(shí)現(xiàn)矩陣轉(zhuǎn)置的方法

    C#實(shí)現(xiàn)矩陣轉(zhuǎn)置的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)矩陣轉(zhuǎn)置的方法,實(shí)例分析了C#針對(duì)矩陣運(yùn)算的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-08-08
  • C#快速實(shí)現(xiàn)拖放操作

    C#快速實(shí)現(xiàn)拖放操作

    這篇文章介紹了C#快速實(shí)現(xiàn)拖放操作的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • Unity調(diào)用打印機(jī)打印圖片

    Unity調(diào)用打印機(jī)打印圖片

    這篇文章主要為大家詳細(xì)介紹了Unity通過(guò)調(diào)用打印機(jī)打印圖片的代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-10-10

最新評(píng)論

浪卡子县| 昌江| 龙游县| 集安市| 兴海县| 大兴区| 芜湖市| 商城县| 长垣县| 黄山市| 澜沧| 兴安县| 泰安市| 江陵县| 内黄县| 庆元县| 泰安市| 蒙城县| 吴忠市| 金溪县| 元谋县| 宝应县| 沾益县| 高尔夫| 遂溪县| 蕉岭县| 渭源县| 开化县| 县级市| 丽江市| 高安市| 交口县| 阿克苏市| 雷州市| 牡丹江市| 阿坝| 怀安县| 壤塘县| 长岛县| 白银市| 佛冈县|