C#如何实现自动更新本地程序的实例分析


本文摘自PHP中文网,作者黄舟,侵删。

关于系统的自动更新。近日有一情况是需要将java端后台最新版本的系统文件覆盖本地客户端,简称自动更新了。

本地会获取当前系统的版本号去请求后台java的接口数据。返回给我的是后台压缩包转的base64字节流。

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


1

2

3

4

if (UpdateSystem(Path.Combine(Application.StartupPath, "Version.txt"), Path.Combine(Application.StartupPath, "u.zip")))

        {

            Application.Exit();

        }


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

/// <summary>

        /// 读取本地版本请求更新

        /// </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>();

                //获取文件内的版本号

                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数据

                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">接受更新包的字节信息</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节中的配置内容

        /// </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;

        }


1

byte[] bytesFile = Convert.FromBase64String(obj["byteArray"].ToString());

这里是拿到的字节流了。

这个方法可以解决在同一个解决方案中有多个项目可以读取到同一个项目下的App.config文件。

注意:其中有引用到的类库, 这是是用来操作压缩包的。

说下思路:第一步其实就是拿到压缩包的字节流再保存到本地,第二步就是循环读取压缩包的文件替换本地的文件,完成本地系统的版本更新。

无论简单与复杂,都需一步步向前方迈进。

以上就是C#如何实现自动更新本地程序的实例分析的详细内容!

相关阅读 >>

在linux下搭建.net core开发环境教程

c#中发送邮件的实现方法详解

c#中enum与string的相互转换的示例

关于.net后台定时服务框架的简单介绍

.net中core如何利用redis发布订阅的实例分析

asp.net开发实用工具

c#实现添加word文本与图片超链接的方法

ants performance profiler(.net性能调优教程)

分享在c#中时间戳是怎么转换的?

c#中关于rabbitmq应用的图文代码详解

更多相关阅读请进入《csharp》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...