Browse Source

自动更新项目

章政 7 years ago
parent
commit
133a9f6b0c

+ 26 - 12
UAS_AutoUpdate/CheckUpdateWindow.Designer.cs

@@ -28,31 +28,45 @@
         /// </summary>
         private void InitializeComponent()
         {
-            this.progressBar1 = new System.Windows.Forms.ProgressBar();
+            this._progressbar = new System.Windows.Forms.ProgressBar();
+            this._processrate = new System.Windows.Forms.Label();
             this.SuspendLayout();
             // 
-            // progressBar1
+            // _progressbar
             // 
-            this.progressBar1.Location = new System.Drawing.Point(51, 56);
-            this.progressBar1.Name = "progressBar1";
-            this.progressBar1.Size = new System.Drawing.Size(253, 23);
-            this.progressBar1.TabIndex = 0;
+            this._progressbar.Location = new System.Drawing.Point(44, 56);
+            this._progressbar.Name = "_progressbar";
+            this._progressbar.Size = new System.Drawing.Size(253, 23);
+            this._progressbar.TabIndex = 0;
             // 
-            // Form1
+            // _processrate
+            // 
+            this._processrate.AutoSize = true;
+            this._processrate.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
+            this._processrate.Location = new System.Drawing.Point(307, 57);
+            this._processrate.Name = "_processrate";
+            this._processrate.Size = new System.Drawing.Size(0, 21);
+            this._processrate.TabIndex = 1;
+            // 
+            // CheckUpdateWindow
             // 
             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
-            this.ClientSize = new System.Drawing.Size(355, 142);
-            this.Controls.Add(this.progressBar1);
-            this.Name = "Form1";
-            this.Text = "Form1";
+            this.ClientSize = new System.Drawing.Size(373, 142);
+            this.Controls.Add(this._processrate);
+            this.Controls.Add(this._progressbar);
+            this.Name = "CheckUpdateWindow";
+            this.Text = "检测升级";
+            this.Load += new System.EventHandler(this.CheckUpdateWindow_Load);
             this.ResumeLayout(false);
+            this.PerformLayout();
 
         }
 
         #endregion
 
-        private System.Windows.Forms.ProgressBar progressBar1;
+        private System.Windows.Forms.ProgressBar _progressbar;
+        private System.Windows.Forms.Label _processrate;
     }
 }
 

+ 82 - 8
UAS_AutoUpdate/CheckUpdateWindow.cs

@@ -1,10 +1,7 @@
-using System;
-using System.Collections.Generic;
-using System.ComponentModel;
-using System.Data;
-using System.Drawing;
-using System.Linq;
-using System.Text;
+using ICSharpCode.SharpZipLib.Zip;
+using System;
+using System.IO;
+using System.Net;
 using System.Windows.Forms;
 
 namespace UAS_AutoUpdate
@@ -15,5 +12,82 @@ namespace UAS_AutoUpdate
         {
             InitializeComponent();
         }
+
+        private void CheckUpdateWindow_Load(object sender, EventArgs e)
+        {
+            //使用WebClient从指定位置下载文件,然后进行解压缩覆盖
+            MessageBox.Show(Application.StartupPath);
+            WebClient wc = new WebClient();
+            wc.DownloadProgressChanged += Wc_DownloadProgressChanged;
+            wc.DownloadFileAsync(new Uri("http://218.17.158.219:8888/Debug.zip"), "Debug.zip");
+        }
+
+        private void Wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
+        {
+            Action act = () =>
+            {
+                _progressbar.Value = e.ProgressPercentage;
+                _processrate.Text = e.ProgressPercentage + "%";
+            };
+            if (IsHandleCreated)
+                this.Invoke(act);
+            if (e.ProgressPercentage == 100)
+            {
+                ZipHelper.UnZip(Application.StartupPath + @"\Debug.zip", @"F:\TEST");
+            }
+            //Close();
+        }
+    }
+
+    public class ZipHelper
+    {
+        /// <summary>
+        /// 用于解压缩Zip文件
+        /// </summary>
+        /// <param name="ZipFilePath"></param>
+        /// <param name="UnZipPath"></param>
+        public static void UnZip(string ZipFilePath, string UnZipPath)
+        {
+            if (!File.Exists(ZipFilePath))
+            {
+                throw new FileNotFoundException(string.Format("未能找到文件 '{0}' ", ZipFilePath));
+            }
+
+            if (!Directory.Exists(UnZipPath))
+            {
+                Directory.CreateDirectory(UnZipPath);
+            }
+
+            using (var s = new ZipInputStream(File.OpenRead(ZipFilePath)))
+            {
+                ZipEntry theEntry;
+                while ((theEntry = s.GetNextEntry()) != null)
+                {
+                    if (theEntry.IsDirectory)
+                    {
+                        continue;
+                    }
+                    string directorName = Path.Combine(UnZipPath, Path.GetDirectoryName(theEntry.Name));
+                    string fileName = Path.Combine(directorName, Path.GetFileName(theEntry.Name));
+                    if (!Directory.Exists(directorName))
+                    {
+                        Directory.CreateDirectory(directorName);
+                    }
+                    if (!string.IsNullOrEmpty(fileName))
+                    {
+                        using (FileStream streamWriter = File.Create(fileName))
+                        {
+                            int size = 4096;
+                            byte[] data = new byte[size];
+                            while (size > 0)
+                            {
+                                streamWriter.Write(data, 0, size);
+                                size = s.Read(data, 0, data.Length);
+                            }
+                        }
+                    }
+                }
+            }
+        }
     }
-}
+}

+ 48 - 3
UAS_AutoUpdate/Program.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Security.Principal;
 using System.Windows.Forms;
 
 namespace UAS_AutoUpdate
@@ -13,9 +14,53 @@ namespace UAS_AutoUpdate
         [STAThread]
         static void Main()
         {
-            Application.EnableVisualStyles();
-            Application.SetCompatibleTextRenderingDefault(false);
-            Application.Run(new CheckUpdateWindow());
+            try
+            {
+                WindowsIdentity identity = WindowsIdentity.GetCurrent();
+                WindowsPrincipal principal = new WindowsPrincipal(identity);
+                //设置应用程序处理异常方式:ThreadException处理
+                Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
+                //处理UI线程异常
+                Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
+                //处理非UI线程异常
+                AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
+                #region 应用程序的主入口点
+                Application.EnableVisualStyles();
+                Application.SetCompatibleTextRenderingDefault(false);
+                string sysdisc = Environment.GetEnvironmentVariable("windir").Substring(0, 1);
+                //创建标签缓存的文件夹
+
+                if (principal.IsInRole(WindowsBuiltInRole.Administrator))
+                    Application.Run(new CheckUpdateWindow());
+                else
+                {
+                    //创建启动对象
+                    System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
+                    // 设置运行文件
+                    startInfo.FileName = Application.ExecutablePath;
+                    //设置启动动作,确保以管理员身份运行
+                    startInfo.Verb = "runas";
+                    //如果不是管理员,则启动UAC
+                    System.Diagnostics.Process.Start(startInfo);
+                }
+                #endregion
+            }
+            catch (Exception ex)
+            {
+                MessageBox.Show(ex.Message, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
+            }
+        }
+
+        //处理线程的异常
+        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
+        {
+            MessageBox.Show(e.Exception.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
+        }
+
+        //未处理的异常统一通过这里返回
+        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
+        {
+            MessageBox.Show(e.ExceptionObject.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
         }
     }
 }

+ 5 - 0
UAS_AutoUpdate/UAS_AutoUpdate.csproj

@@ -49,7 +49,12 @@
   <PropertyGroup>
     <ApplicationIcon>Resources\update.ico</ApplicationIcon>
   </PropertyGroup>
+  <PropertyGroup />
+  <PropertyGroup />
   <ItemGroup>
+    <Reference Include="ICSharpCode.SharpZipLib">
+      <HintPath>tool\ICSharpCode.SharpZipLib.dll</HintPath>
+    </Reference>
     <Reference Include="System" />
     <Reference Include="System.Core" />
     <Reference Include="System.Xml.Linq" />

BIN
UAS_AutoUpdate/tool/ICSharpCode.SharpZipLib.dll