Browse Source

修改自动升级程序

章政 7 years ago
parent
commit
7b8b26ad67

+ 136 - 3
UAS_AutoUpdate/CheckUpdateWindow.cs

@@ -1,8 +1,10 @@
 using ICSharpCode.SharpZipLib.Zip;
 using System;
+using System.Diagnostics;
 using System.IO;
 using System.Net;
 using System.Windows.Forms;
+using System.Xml;
 
 namespace UAS_AutoUpdate
 {
@@ -11,6 +13,10 @@ namespace UAS_AutoUpdate
 
         public static bool Zipped = false;
 
+        string ConfigFile = "Config.xml";
+
+        string ServerConfigFile = "ServerConfig.xml";
+
         public CheckUpdateWindow()
         {
             InitializeComponent();
@@ -19,11 +25,137 @@ namespace UAS_AutoUpdate
 
         private void CheckUpdateWindow_Load(object sender, EventArgs e)
         {
-
             //使用WebClient从指定位置下载文件,然后进行解压缩覆盖
+            FileInfo info = new FileInfo(ConfigFile);
+            if (info.Length == 0)
+            {
+                XmlDocument doc = new XmlDocument();
+                //创建类型声明节点  
+                XmlNode node = doc.CreateXmlDeclaration("1.0", "utf-8", "");
+                doc.AppendChild(node);
+                //创建根节点  
+                XmlElement xeRoot = doc.CreateElement("cacheInfo");
+                doc.AppendChild(xeRoot);
+                doc.Save(ConfigFile);
+            }
             WebClient wc = new WebClient();
-            wc.DownloadProgressChanged += Wc_DownloadProgressChanged;
-            wc.DownloadFileAsync(new Uri("http://172.16.11.99/UAS_MES.zip"), "UAS_MES.zip");
+            wc.DownloadFile(new Uri(GetCacheData(ConfigFile, "ServerConfigPath").ToString()), ServerConfigFile);
+            //服务器获取的配置文件
+            Version ver1 = new Version(GetCacheData(ServerConfigFile, "Version").ToString());
+            //本地的配置文件
+            Version ver2 = new Version(GetCacheData(ConfigFile, "Version").ToString());
+            //进行版本的比较
+            if (ver1 > ver2)
+            {
+                wc.DownloadProgressChanged += Wc_DownloadProgressChanged;
+                wc.DownloadFileAsync(new Uri(GetCacheData(ConfigFile, "UpdatePath").ToString()), "UAS_MES.zip");
+                SetCacheData(ConfigFile, "Version", ver1.ToString());
+            }
+            else
+            {
+                Process p = Process.Start("UAS_MES.exe");
+                Close();
+            }
+        }
+
+        public static void SetCacheData(string FileName, string ParamName, object Value)
+        {
+            try
+            {
+                //根据地址读取xml文件
+                XmlDocument doc = new XmlDocument();
+                XmlReaderSettings settings = new XmlReaderSettings { CheckCharacters = false };
+                //忽略文档里面的注释
+                settings.IgnoreComments = true;
+                XmlReader reader = XmlReader.Create(FileName, settings);
+                doc.Load(reader);
+                //先得到根节点
+                XmlNode rootNode = doc.SelectSingleNode("cacheInfo");
+                //再由根节点去找制定的节点
+                XmlNodeList nodeList = rootNode.ChildNodes;
+                bool flag = false;
+                foreach (XmlNode node in nodeList)
+                {
+                    //找到了这个节点名字
+                    if (node.Name == ParamName)
+                    {
+                        //就直接赋值
+                        node.InnerText = Value.ToString();
+                        flag = true;
+                    }
+                }
+                //如果没有该节点,就创建节点保存结果
+                if (!flag)
+                {
+                    //创建节点
+                    XmlElement newNode = doc.CreateElement(ParamName);
+                    XmlAttribute attr = doc.CreateAttribute("Type");
+                    attr.InnerText = Value.GetType().ToString();
+                    newNode.InnerText = Value.ToString();
+                    newNode.SetAttributeNode(attr);
+                    //讲新建的节点挂到根节点上
+                    rootNode.AppendChild(newNode);
+                }
+                //关闭Reader
+                reader.Close();
+                doc.Save(FileName);
+            }
+            catch (Exception)
+            {
+
+            }
+        }
+
+        public static object GetCacheData(string FileName, string ParamName)
+        {
+            try
+            {
+                object o = null;
+                //根据地址读取xml文件
+                XmlDocument doc = new XmlDocument();
+                XmlReaderSettings settings = new XmlReaderSettings { CheckCharacters = false };
+                //忽略文档里面的注释
+                settings.IgnoreComments = true;
+                XmlReader reader = XmlReader.Create(FileName, settings);
+                doc.Load(reader);
+                //先得到根节点
+                XmlNode rootNode = doc.SelectSingleNode("cacheInfo");
+                //再由根节点去找制定的节点
+                XmlNodeList nodeList = rootNode.ChildNodes;
+                foreach (XmlNode node in nodeList)
+                {
+                    //找到了这个节点名字
+                    if (node.Name == ParamName)
+                    {
+                        //返回节点的内容
+                        switch (((XmlElement)node).GetAttribute("Type"))
+                        {
+                            case "System.String":
+                                o = node.InnerText;
+                                break;
+                            case "System.Int32":
+                                o = int.Parse(node.InnerText);
+                                break;
+                            case "System.Boolean":
+                                o = node.InnerText == "True" ? true : false;
+                                break;
+                            default:
+                                break;
+                        }
+                        break;
+                    }
+                }
+                //关闭reader
+                reader.Close();
+                if (o == null)
+                    return "";
+                else
+                    return o;
+            }
+            catch (Exception)
+            {
+                return "";
+            }
         }
 
         private void Wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
@@ -106,6 +238,7 @@ namespace UAS_AutoUpdate
                 }
                 File.Delete(Application.StartupPath + @"\UAS_MES.zip");
                 CheckUpdateWindow.Zipped = true;
+                Process p = Process.Start("UAS_MES.exe");
             }
         }
     }

+ 10 - 0
UAS_AutoUpdate/Properties/Resources.Designer.cs

@@ -60,6 +60,16 @@ namespace UAS_AutoUpdate.Properties {
             }
         }
         
+        /// <summary>
+        ///   查找类似于 (图标) 的 System.Drawing.Icon 类型的本地化资源。
+        /// </summary>
+        internal static System.Drawing.Icon U_Icon {
+            get {
+                object obj = ResourceManager.GetObject("U_Icon", resourceCulture);
+                return ((System.Drawing.Icon)(obj));
+            }
+        }
+        
         /// <summary>
         ///   查找类似于 (图标) 的 System.Drawing.Icon 类型的本地化资源。
         /// </summary>

+ 3 - 0
UAS_AutoUpdate/Properties/Resources.resx

@@ -121,4 +121,7 @@
   <data name="update" type="System.Resources.ResXFileRef, System.Windows.Forms">
     <value>..\Resources\update.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
   </data>
+  <data name="U_Icon" type="System.Resources.ResXFileRef, System.Windows.Forms">
+    <value>..\Resources\U_Icon.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
+  </data>
 </root>

BIN
UAS_AutoUpdate/Resources/U_Icon.ico


+ 6 - 2
UAS_AutoUpdate/UAS_AutoUpdate.csproj

@@ -11,6 +11,7 @@
     <AssemblyName>CheckUpdate</AssemblyName>
     <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
     <FileAlignment>512</FileAlignment>
+    <IsWebBootstrapper>false</IsWebBootstrapper>
     <PublishUrl>publish\</PublishUrl>
     <Install>true</Install>
     <InstallFrom>Disk</InstallFrom>
@@ -23,7 +24,6 @@
     <MapFileExtensions>true</MapFileExtensions>
     <ApplicationRevision>0</ApplicationRevision>
     <ApplicationVersion>1.0.0.%2a</ApplicationVersion>
-    <IsWebBootstrapper>false</IsWebBootstrapper>
     <UseApplicationTrust>false</UseApplicationTrust>
     <BootstrapperEnabled>true</BootstrapperEnabled>
   </PropertyGroup>
@@ -47,7 +47,7 @@
     <WarningLevel>4</WarningLevel>
   </PropertyGroup>
   <PropertyGroup>
-    <ApplicationIcon>Resources\update.ico</ApplicationIcon>
+    <ApplicationIcon>U_Icon.ico</ApplicationIcon>
   </PropertyGroup>
   <PropertyGroup />
   <PropertyGroup />
@@ -114,6 +114,10 @@
   <ItemGroup>
     <None Include="Resources\update.ico" />
   </ItemGroup>
+  <ItemGroup>
+    <Content Include="Resources\U_Icon.ico" />
+    <Content Include="U_Icon.ico" />
+  </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
        Other similar extension points exist, see Microsoft.Common.targets.

BIN
UAS_AutoUpdate/U_Icon.ico