Pārlūkot izejas kodu

WebClient下载Zip文件解压

章政 8 gadi atpakaļ
vecāks
revīzija
1ac0dfc0d4
2 mainītis faili ar 140 papildinājumiem un 4 dzēšanām
  1. 39 3
      UAS-MES/Form1.Designer.cs
  2. 101 1
      UAS-MES/Form1.cs

+ 39 - 3
UAS-MES/Form1.Designer.cs

@@ -30,6 +30,9 @@
         {
             this.button1 = new System.Windows.Forms.Button();
             this.button2 = new System.Windows.Forms.Button();
+            this.button3 = new System.Windows.Forms.Button();
+            this.button4 = new System.Windows.Forms.Button();
+            this.progressBar1 = new System.Windows.Forms.ProgressBar();
             this.SuspendLayout();
             // 
             // button1
@@ -52,12 +55,42 @@
             this.button2.Text = "button2";
             this.button2.UseVisualStyleBackColor = true;
             // 
+            // button3
+            // 
+            this.button3.Location = new System.Drawing.Point(305, 158);
+            this.button3.Name = "button3";
+            this.button3.Size = new System.Drawing.Size(75, 23);
+            this.button3.TabIndex = 0;
+            this.button3.Text = "button3";
+            this.button3.UseVisualStyleBackColor = true;
+            this.button3.Click += new System.EventHandler(this.button3_Click_1);
+            // 
+            // button4
+            // 
+            this.button4.Location = new System.Drawing.Point(410, 158);
+            this.button4.Name = "button4";
+            this.button4.Size = new System.Drawing.Size(75, 23);
+            this.button4.TabIndex = 2;
+            this.button4.Text = "button4";
+            this.button4.UseVisualStyleBackColor = true;
+            this.button4.Click += new System.EventHandler(this.button4_Click);
+            // 
+            // progressBar1
+            // 
+            this.progressBar1.ForeColor = System.Drawing.SystemColors.ControlDarkDark;
+            this.progressBar1.Location = new System.Drawing.Point(216, 90);
+            this.progressBar1.Name = "progressBar1";
+            this.progressBar1.Size = new System.Drawing.Size(336, 23);
+            this.progressBar1.TabIndex = 3;
+            // 
             // Form1
             // 
-            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
+            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
-            this.ClientSize = new System.Drawing.Size(1239, 738);
-            this.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
+            this.ClientSize = new System.Drawing.Size(929, 590);
+            this.Controls.Add(this.progressBar1);
+            this.Controls.Add(this.button4);
+            this.Controls.Add(this.button3);
             this.Name = "Form1";
             this.Tag = "123123";
             this.Text = "Form1";
@@ -73,5 +106,8 @@
         private CustomControl.ButtonUtil.NormalButton normalButton1;
         private CustomControl.TextBoxWithIcon.EnterTextBox enterTextBox1;
         private CustomControl.SetLoading.LoadingCircle loadingCircle1;
+        private System.Windows.Forms.Button button3;
+        private System.Windows.Forms.Button button4;
+        private System.Windows.Forms.ProgressBar progressBar1;
     }
 }

+ 101 - 1
UAS-MES/Form1.cs

@@ -9,10 +9,16 @@ using System.Text;
 using System.Text.RegularExpressions;
 using System.Windows.Forms;
 using System.Windows.Forms.DataVisualization.Charting;
+using System.Web.ClientServices;
 using UAS_MES.CustomControl.DataGrid_View;
 using UAS_MES.DataOperate;
 using UAS_MES.PublicForm;
 using UAS_MES.PublicMethod;
+using System.Net;
+using System.IO;
+using System.Reflection;
+using System.Threading;
+using UAS_MES.CustomControl;
 
 namespace UAS_MES
 {
@@ -45,9 +51,103 @@ namespace UAS_MES
 
         private void Form1_Load(object sender, EventArgs e)
         {
-        
 
         }
 
+        private void button3_Click_1(object sender, EventArgs e)
+        {
+            // 禁用按钮
+            //this.button1.Enabled = false;
+
+            //// 实例化业务对象
+            //PublicMethod.LongTimeWork workder = new PublicMethod.LongTimeWork();
+
+            //workder.ValueChanged += new PublicMethod.ValueChangedEventHandler(workder_ValueChanged);
+
+            //// 使用异步方式调用长时间的方法
+            //Action handler = new Action(workder.LongTimeMethod);
+            //handler.BeginInvoke(new AsyncCallback(this.AsyncCallback), handler);
+            WebClient wc = new WebClient();
+            wc.DownloadProgressChanged += Wc_DownloadProgressChanged;
+            wc.DownloadFileAsync(new Uri("http://218.17.158.219:8888/UAS_WinForm.zip"), "UAS_WinForm.zip");
+            //processBar1.Run();
+        }
+
+        // 结束异步操作
+        private void AsyncCallback(IAsyncResult ar)
+        {
+            // 标准的处理步骤
+            Action handler = ar.AsyncState as Action;
+            handler.EndInvoke(ar);
+            MessageBox.Show("工作完成!");
+            System.Windows.Forms.MethodInvoker invoker = () =>
+            {
+                // 重新启用按钮
+                this.button3.Enabled = true;
+            };
+            if (this.InvokeRequired)
+            {
+                this.Invoke(invoker);
+            }
+            else
+            {
+                invoker();
+            }
+        }
+
+        private void Wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
+        {
+            Action act = () =>
+            {
+                this.progressBar1.Value = e.ProgressPercentage;
+                //this.label1.Text = e.ProgressPercentage + "%";
+            };
+            if (e.ProgressPercentage == 100) {
+                ZipHelper.UnZip(@"H:\UAS_WinForm\UAS-MES\bin\Debug\icsharpcode-SharpZipLib-4ad264b.zip", @"D:\");
+            }
+            this.Invoke(act);
+        }
+
+        //public static void Unzip()
+        //{
+        //    string _appPath = new DirectoryInfo(Assembly.GetExecutingAssembly().ManifestModule.FullyQualifiedName).Parent.FullName;
+        //    string s7z = _appPath + @"\7-Zip\7z.exe";
+        //    System.Diagnostics.Process pNew = new System.Diagnostics.Process();
+        //    pNew.StartInfo.FileName = s7z;
+        //    pNew.StartInfo.Arguments = string.Format(" x \"{0}\\{1}\" -y -o\"{0}\"", _appPath, zipFileFullName);
+        //    //x "1" -y -o"2" 这段7z命令的意思: x是解压的意思 "{0}"的位置写要解压文件路径"{1}"这个1的位置写要解压的文件名 -y是覆盖的意思 -o是要解压的位置
+        //    pNew.Start();
+        //    //等待完成
+        //    pNew.WaitForExit();
+        //    //删除压缩包
+        //    File.Delete(_appPath + @"\" + zipFileFullName);
+        //}
+
+        private void button4_Click(object sender, EventArgs e)
+        {
+            WebClient wc = new WebClient();
+            wc.DownloadProgressChanged += Wc_DownloadProgressChanged;
+            wc.DownloadFileAsync(new Uri("http://218.17.158.219:8888/UAS_WinForm.zip"), "UAS_WinForm.zip");
+          
+        }
     }
+
+    //public class ZipHelper
+    //{
+    //    public static string zipFileFullName = "UAS_WinForm.zip";
+    //    public static void Unzip()
+    //    {
+    //        string _appPath = new DirectoryInfo(Assembly.GetExecutingAssembly().ManifestModule.FullyQualifiedName).Parent.FullName;
+    //        string s7z = _appPath + @"\7z.exe";
+    //        System.Diagnostics.Process pNew = new System.Diagnostics.Process();
+    //        pNew.StartInfo.FileName = s7z;
+    //        pNew.StartInfo.Arguments = string.Format(" x \"{0}\\{1}\" -y -o\"{0}\"", _appPath, zipFileFullName);
+    //        //x "1" -y -o"2" 这段7z命令的意思: x是解压的意思 "{0}"的位置写要解压文件路径"{1}"这个1的位置写要解压的文件名 -y是覆盖的意思 -o是要解压的位置
+    //        pNew.Start();
+    //        //等待完成
+    //        pNew.WaitForExit();
+    //        //删除压缩包
+    //        //File.Delete(_appPath + @"\" + zipFileFullName);
+    //    }
+    //}
 }