/// <summary> /// 上传数据完成回调 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Client_UploadDataCompleted(object sender, System.Net.UploadDataCompletedEventArgs e) { if (e.Error != null) { Debug.Log(e.Error.ToString()); } Debug.Log(Convert.ToString(System.Text.Encoding.UTF8.GetString(e.Result))); DoNext(); }
public static void OnUploadDataCompleted(object sender, System.Net.UploadDataCompletedEventArgs e) { if (e.Cancelled == true) { // MessageBox.Show("Download has been canceled."); System.Console.WriteLine("Download has been canceled."); return; } else if (e.Error != null) { throw e.Error; } byte[] ba = e.Result; System.Console.WriteLine(ba != null); string userState = (string)e.UserState; System.Console.WriteLine("UserState: \"{0}\".", userState); }
protected virtual void OnUploadDataCompleted(System.Net.UploadDataCompletedEventArgs e) { }
private void Client_UploadDataCompleted(object sender, System.Net.UploadDataCompletedEventArgs e) { //内容类型不是流,则为服务端出错 if (((System.Net.WebClient)sender).ResponseHeaders == null || ((System.Net.WebClient)sender).ResponseHeaders["Content-Type"] != "application/octet-stream" || e.Error != null) { //出现异常 this.IsEnabledWindow = false; this.IsError = true; Application.Current.Dispatcher.Invoke(() => { UpdateFailedWindow.Instance().UpdateVersion = this._version; UpdateFailedWindow.Instance().CurVersion = this._curVersion; UpdateFailedWindow.Instance().ShowDialog(); }); return; } this.Progress = 100.00M; this.Description = "文件下载完毕"; bool isUpdateSuccess = false; //备份的集合 List <string> backs = new List <string>(); //新增的集合 List <string> adds = new List <string>(); string tempPath = App.TempPath; try { if (File.Exists("myzip.zip")) { File.Delete("myzip.zip"); } this.Description = $"正在解压升级文件"; //下载的临时文件路径 Directory.CreateDirectory(tempPath); File.WriteAllBytes("myzip.zip", e.Result); using (ZipFile zip = new ZipFile("myzip.zip")) { zip.ExtractAll(tempPath, ExtractExistingFileAction.OverwriteSilently); } //正在运行的实例 KillMainProcess(); if (App.ReInstall) { string installpath = $"{tempPath}\\IM_install.exe"; System.Diagnostics.Process.Start(installpath); //Environment.Exit(0); this.IsEnabledWindow = false; return; } this.Description = $"开始安装升级文件"; foreach (var item in verInfo.FileInfos) { this.Description = $"正在安装升级文件:{item.FileRelativePath}"; var info = new FileInfo(Path.Combine("temp", item.FileRelativePath)); //若存在,则备份后替换,否则直接拷贝 if (File.Exists(item.FileRelativePath)) { string backPath = $"{item.FileRelativePath}_back"; //if(item.FileRelativePath==) backs.Add(backPath); info.Replace(item.FileRelativePath, backPath); } else { if (item.FileRelativePath.Contains("\\")) { string fileName = item.FileRelativePath.Split('\\').LastOrDefault(); string childDic = item.FileRelativePath.Remove(item.FileRelativePath.Length - fileName.Length); Directory.CreateDirectory(childDic); } info.CopyTo(item.FileRelativePath, true); adds.Add(item.FileRelativePath); } } isUpdateSuccess = true; } catch (Exception ex) { Application.Current.Dispatcher.Invoke(() => { this.Description = $"升级文件失败,失败原因:{ex.Message}"; }); System.Diagnostics.Debug.WriteLine($"升级文件失败,失败原因:{ex.Message}"); } if (isUpdateSuccess) { App.VersionNumber = verInfo.LatestVersion.ToString(); App.Externalversion = verInfo.VersionName; Installed = true; //更新成功,删除所有备份的文件 try { foreach (string back in backs) { try { File.Delete(back); } catch (Exception ex) { } } } catch (Exception ex) { string str = ex.Message; //System.Diagnostics.Debug.WriteLine(ex.Message); } } else { //更新失败,恢复文件 foreach (string back in backs) { string old = back.Remove(back.LastIndexOf("_back")); File.Copy(back, old, true); File.Delete(back); } //删除新增的文件 foreach (string add in adds) { File.Delete(add); } Installed = false; UserOldVersion = true; } //删除临时文件夹 if (Directory.Exists(tempPath)) { System.IO.Directory.Delete(tempPath, true); } }