1
yhj
2024-07-24 5e5d945e91568b973faa27d8ab0bcef99fc4a6c5
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#region
 
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using CSFrameworkV5.Common;
using CSFrameworkV5.Core;
 
#endregion
 
namespace CSFrameworkV5.Library.CommonClass
{
    /// <summary>
    ///     版本检测工具类, LAST UPDATE:20211212
    /// </summary>
    public class VersionCheckNEW
    {
        //等待升级程序安全退出时间,单位毫秒。
        private const int WAIT_SECONDS = 3000;
 
        //系统常量定义 - 不可更改
        private const string
            Arg_CheckVersion = "CheckVersion"; // 主程序调用升级程序检查是否有版本更新
 
        private const string Arg_MainEXECall = "MainEXECall"; // 主程序调用升级程序   
        private const string UpgraderINIFile = "UpgraderClient.ini"; //本地升级配置文件
 
        private const string
            UpgraderServerINIFile = "UpgraderServer.ini"; //服务端配置文件
 
        //==========================================================================================
        //重要变量定义!!!!!
 
        //主程序文件名。主程序调用升级程序,必须修改此名称!
        private const string DEF_MAIN_PROGRAM_NAME = "CSFrameworkV5.Main.exe";
 
        //CSFramework商业版升级程序文件名
        private const string DEF_UPGRADER_NAME = "CSFramework.AutoUpgrader.exe";
 
        //CSFramework商业版升级程序 - 控制类的名字空间
        private const string DEF_UPGRADER_CONTROLLER =
            "CSFramework.AutoUpgrader.UpgraderController";
 
        //V5.0标配升级程序文件名
        //const string DEF_UPGRADER_NAME = "CSFrameworkV5.AutoUpgrader.exe";
 
        //V5.0标配升级程序控制类的名字空间
        //const string DEF_UPGRADER_CONTROLLER = "CSFramework.AutoUpgrader.Library.UpgraderController"; 
 
        //
        //==========================================================================================
 
        /// <summary>
        ///     自动升级
        /// </summary>
        public static void AutoUpgrader(ref bool exitApp)
        {
            var file = Application.StartupPath + @"\" + DEF_UPGRADER_NAME;
 
            //本地没有升级程序文件,不进行自我升级, UpgraderClient.exe是通过DownloaderDB下载的
            if (!File.Exists(file)) return;
 
            frmWaitingEx.ShowMe(null, "正在获取最新的升级程序.......");
            CheckUpgraderByDownloader();
 
            frmWaitingEx.ShowMe(null, "正在获取升级包.......");
            CheckVersion(ref exitApp);
        }
 
        /// <summary>
        ///     检查版本升级程序,有新版本自动下载
        /// </summary>
        /// <returns>True:有新版本, False:无新版本</returns>
        public static bool CheckUpgraderByDownloader()
        {
            var upgraderPath =
                Application.StartupPath + @"\" + DEF_UPGRADER_NAME;
            var serverVer = "";
 
            try
            {
                //获取升级程序的程序集
                var fileData = File.ReadAllBytes(upgraderPath);
                var upgrader = Assembly.Load(fileData);
 
                //通过.NET反射机制创建UpgraderController实例
                var t = upgrader.GetType(DEF_UPGRADER_CONTROLLER);
                var o = t.Assembly.CreateInstance(t.FullName);
                var M = t.GetMethod("DownloadUpgrader");
 
                //调用DownloadUpgrader方法
                //参数名:destPath,传值:upgraderPath==Application.StartupPath                                
                var r = M.Invoke(o, new object[] { upgraderPath });
 
                serverVer = r.ToStringEx();
                if (serverVer != "") //有新版本,保存配置文件
                {
                    var ini = new IniFile(Application.StartupPath + @"\" +
                                          UpgraderINIFile);
                    ini.IniWriteValue("Setup", "UpgraderFileVersion",
                        serverVer
                            .ToStringEx()); //写入版本号                                        
                }
            }
            catch (Exception ex)
            {
                LogUserOperate.Write(ex);
                Msg.Warning("检查升级程序失败!\r\n" + ex.Message);
            }
 
            return serverVer != "";
        }
 
        /// <summary>
        ///     检查更新,有更新要退出主程序
        /// </summary>
        /// <param name="AppExit">退出应用程序</param>
        public static void CheckVersion(ref bool AppExit)
        {
            AppExit = false;
            try
            {
                var upgraderPath = Path.Combine(Application.StartupPath,
                    DEF_UPGRADER_NAME);
 
                //获取升级程序的程序集
                var fileData = File.ReadAllBytes(upgraderPath);
                var upgrader = Assembly.Load(fileData);
 
                //通过.NET反射机制创建UpgraderController实例
                var t = upgrader.GetType(DEF_UPGRADER_CONTROLLER);
                var o = t.Assembly.CreateInstance(t.FullName);
 
                //获取升级包数量
                var M = t.GetMethod("GetNewlyPackageCount");
                var ret = M.Invoke(o, null);
                var count = ConvertEx.ToInt(ret);
 
                frmWaitingEx.HideMe();
 
                if (count > 0) //有新版本,升级包的文件数>0
                {
                    AppExit = true;
 
                    //启动通用升级程序(商业版)。程序参数:MainEXECall YourERP.exe
                    Process.Start(upgraderPath,
                        Arg_MainEXECall + " " + DEF_MAIN_PROGRAM_NAME);
                }
            }
            catch (Exception ex)
            {
                LogUserOperate.Write(ex);
                Msg.ShowError("检查版本失败!CheckVersion:" + ex.Message);
            }
        }
 
        /// <summary>
        ///     取本地升级程序的版本号
        /// </summary>
        /// <returns></returns>
        public static string GetUpgraderVersion()
        {
            var ini =
                new IniFile(Application.StartupPath + @"\" + UpgraderINIFile);
            var clientVer = ini.IniReadValue("Client", "UpgraderFileVersion");
            return clientVer;
        }
 
        /// <summary>
        ///     保存升级程序版本号
        /// </summary>
        /// <param name="version">版本号</param>
        public static void SaveUpgraderVersion(string version)
        {
            var ini =
                new IniFile(Application.StartupPath + @"\" + UpgraderINIFile);
            ini.IniWriteValue("Client", "UpgraderFileVersion", version);
        }
    }
}