Пример #1
0
 public static UpdateFailedWindow Instance()
 {
     lock (ObjLok)
     {
         return(_instance ?? (_instance = new UpdateFailedWindow()));
     }
 }
Пример #2
0
        void GetPcIncrementFiles(string param)
        {
            try
            {
                System.Diagnostics.Debug.WriteLine($"清单文件:{param}");
                System.Net.WebClient client = new System.Net.WebClient();
                client.UploadProgressChanged += Client_UploadProgressChanged;;
                client.UploadDataCompleted   += Client_UploadDataCompleted;;
                client.Headers.Add(System.Net.HttpRequestHeader.ContentType, "application/json");
                client.UploadDataAsync(new Uri(App.PcIncrementFiles), Encoding.UTF8.GetBytes(param));
            }
            catch
            {
                //出现异常
                this.IsEnabledWindow = false;
                this.IsError         = true;

                Application.Current.Dispatcher.Invoke(() =>
                {
                    UpdateFailedWindow.Instance().UpdateVersion = this._version;
                    UpdateFailedWindow.Instance().CurVersion    = this._curVersion;
                    UpdateFailedWindow.Instance().ShowDialog();
                });
            }
        }
Пример #3
0
        public void GetPcVersionDetail(string curVersion)
        {
            //System.Net.WebClient client = new System.Net.WebClient();
            //client.DownloadDataCompleted += Client_DownloadDataCompleted;
            //// client.DownloadProgressChanged += Client_DownloadProgressChanged;
            //client.DownloadDataAsync(new Uri($"{App.PcVersionDetail}={curVersion}"), "GetPcVersionDetail");

            System.Net.Http.HttpClient client = new HttpClient();

            var t1 = client.GetStringAsync($"{App.PcVersionDetail}={curVersion}").ContinueWith(t =>
            {
                if (t.Exception == null)
                {
                    if (!string.IsNullOrWhiteSpace(t.Result))
                    {
                        verInfo = Newtonsoft.Json.JsonConvert.DeserializeObject <VersionInfo>(t.Result);

                        var lst = from item in verInfo.FileInfos
                                  select item.FileRelativePath;
                        var obj = new
                        {
                            FileList = lst
                        };
                        App.ReInstall = lst.Contains("IM_install.exe");
                        GetPcIncrementFiles(Newtonsoft.Json.JsonConvert.SerializeObject(obj));
                    }
                    else
                    {
                        Application.Current.Dispatcher.Invoke(() => this.Description = $"清单文件下载失败,失败原因:{t.Exception?.Message}");
                        //  System.Diagnostics.Debug.WriteLine($"清单文件下载失败,失败原因:{e.Error.Message}");
                        System.Threading.Thread.Sleep(2000);
                        Installed      = false;
                        UserOldVersion = true;
                    }
                }
                else
                {
                    //出现异常
                    this.IsEnabledWindow = false;
                    this.IsError         = true;

                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        UpdateFailedWindow.Instance().UpdateVersion = this._version;
                        UpdateFailedWindow.Instance().CurVersion    = this._curVersion;
                        UpdateFailedWindow.Instance().ShowDialog();
                    });
                }
            });
        }
Пример #4
0
        void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object target)
        {
            switch (connectionId)
            {
            case 1:
                this.root = ((IMLaunch.UpdateFailedWindow)(target));
                return;

            case 2:

            #line 12 "..\..\UpdateFailedWindow.xaml"
                ((System.Windows.Controls.StackPanel)(target)).MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(this.StackPanel_MouseLeftButtonDown);

            #line default
            #line hidden
                return;

            case 3:
                this.btnClose = ((System.Windows.Controls.Button)(target));

            #line 14 "..\..\UpdateFailedWindow.xaml"
                this.btnClose.Click += new System.Windows.RoutedEventHandler(this.btnClose_Click);

            #line default
            #line hidden
                return;

            case 4:

            #line 21 "..\..\UpdateFailedWindow.xaml"
                ((System.Windows.Controls.Button)(target)).Click += new System.Windows.RoutedEventHandler(this.btnClose_Click);

            #line default
            #line hidden
                return;

            case 5:
                this.btnUpdate = ((System.Windows.Controls.Button)(target));

            #line 23 "..\..\UpdateFailedWindow.xaml"
                this.btnUpdate.Click += new System.Windows.RoutedEventHandler(this.btnUpdate_Click);

            #line default
            #line hidden
                return;
            }
            this._contentLoaded = true;
        }
Пример #5
0
        private void btnUpdate_Click(object sender, RoutedEventArgs e)
        {
            this.Hide();

            UpdateViewModel vm  = new UpdateViewModel(UpdateVersion, CurVersion);
            UpdateWindow    win = new UpdateWindow();

            win.DataContext = vm;
            win.ShowDialog();

            if (!vm.IsError)
            {
                Application.Current?.Shutdown(0);
            }
            else
            {
                UpdateFailedWindow.Instance().ShowDialog();
            }

            if (vm.IsStartMainApp)
            {
                System.Diagnostics.Process.Start(string.Format(@"{0}\{1}", AppDomain.CurrentDomain.BaseDirectory, App.MainProgramName));
            }
        }
Пример #6
0
        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);
            }
        }