From 34697d73ce31cfe3edc760f6c4d1d3640ce263a1 Mon Sep 17 00:00:00 2001
From: lg <999544862qq.com>
Date: 星期一, 16 九月 2024 20:53:05 +0800
Subject: [PATCH] 增加更新

---
 DevApp/Gs.DevApp/TestForm/XtraForm1.cs |   56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 55 insertions(+), 1 deletions(-)

diff --git a/DevApp/Gs.DevApp/TestForm/XtraForm1.cs b/DevApp/Gs.DevApp/TestForm/XtraForm1.cs
index 0e083e6..010f87e 100644
--- a/DevApp/Gs.DevApp/TestForm/XtraForm1.cs
+++ b/DevApp/Gs.DevApp/TestForm/XtraForm1.cs
@@ -1,11 +1,15 @@
-锘縰sing DevExpress.XtraEditors;
+锘縰sing AutoUpdaterDotNET;
+using DevExpress.XtraEditors;
+using Gs.DevApp.ToolBox;
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
+using System.Configuration;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
+using System.Threading;
 using System.Threading.Tasks;
 using System.Windows.Forms;
 
@@ -16,6 +20,56 @@
         public XtraForm1()
         {
             InitializeComponent();
+            string _version = ConfigurationManager.AppSettings["Version"];
+            AutoUpdater.InstalledVersion = new Version(_version);
+            AutoUpdater.Start("https://dianbei.top/AutoUpdaterTest.xml");
+            AutoUpdater.CheckForUpdateEvent += AutoUpdater_CheckForUpdateEvent;
+        }
+
+        private void AutoUpdater_CheckForUpdateEvent(UpdateInfoEventArgs args)
+        {
+            if (args != null)
+            {
+                if (args.IsUpdateAvailable)
+                {
+                    string _strMsg = string.Format($@"There is new version {args.CurrentVersion} available. You are using version {args.InstalledVersion}. This is required update. Press Ok to begin updating the application.", @"Update Available");
+                    if (!MsgHelper.AskQuestion(_strMsg))
+                        Application.Exit();
+                    try
+                    {
+                        if (AutoUpdater.DownloadUpdate(args))
+                        {
+                            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
+                            if (config != null)
+                            {
+                                AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
+                                if (appSettings.Settings.AllKeys.Contains("Version"))
+                                {
+                                    appSettings.Settings["Version"].Value = args.CurrentVersion;
+                                }
+                                else
+                                {
+                                    appSettings.Settings.Add("Version", args.CurrentVersion);
+                                }
+                                config.Save(ConfigurationSaveMode.Modified);
+                                ConfigurationManager.RefreshSection("appSettings"); 
+                            }
+                            Application.Exit();
+                        }
+                    }
+                    catch (Exception exception)
+                    {
+                        MessageBox.Show(exception.Message, exception.GetType().ToString(), MessageBoxButtons.OK,
+                            MessageBoxIcon.Error);
+                    }
+                }
+            }
+            else
+            {
+                MessageBox.Show(
+                        @"There is a problem reaching update server please check your internet connection and try again later.",
+                        @"Update check failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
+            }
         }
     }
 }
\ No newline at end of file

--
Gitblit v1.9.3