Browse Source

防止文件占用报错

章政 8 years ago
parent
commit
bea20d215c
1 changed files with 16 additions and 9 deletions
  1. 16 9
      UAS_AutoUpdate/CheckUpdateWindow.cs

+ 16 - 9
UAS_AutoUpdate/CheckUpdateWindow.cs

@@ -23,7 +23,7 @@ namespace UAS_AutoUpdate
             //使用WebClient从指定位置下载文件,然后进行解压缩覆盖
             WebClient wc = new WebClient();
             wc.DownloadProgressChanged += Wc_DownloadProgressChanged;
-            wc.DownloadFileAsync(new Uri("http://218.17.158.219:8888/UAS_MES.zip"), "UAS_MES.zip");
+            wc.DownloadFileAsync(new Uri("http://172.16.11.99/UAS_MES.zip"), "UAS_MES.zip");
         }
 
         private void Wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
@@ -85,21 +85,28 @@ namespace UAS_AutoUpdate
                     }
                     if (!string.IsNullOrEmpty(fileName))
                     {
-                        using (FileStream streamWriter = File.Create(fileName))
+                        try
                         {
-                            int size = 4096;
-                            byte[] data = new byte[size];
-                            while (size > 0)
+                            //防止文件正在使用中报错
+                            using (FileStream streamWriter = File.Create(fileName))
                             {
-                                size = s.Read(data, 0, data.Length);
-                                streamWriter.Write(data, 0, size);
+                                int size = 4096;
+                                byte[] data = new byte[size];
+                                while (size > 0)
+                                {
+                                    size = s.Read(data, 0, data.Length);
+                                    streamWriter.Write(data, 0, size);
+                                }
                             }
                         }
+                        catch (Exception)
+                        {
+                        }
                     }
                 }
+                File.Delete(Application.StartupPath + @"\UAS_MES.zip");
+                CheckUpdateWindow.Zipped = true;
             }
-            File.Delete(Application.StartupPath + @"\UAS_MES.zip");
-            CheckUpdateWindow.Zipped = true;
         }
     }
 }