Exemple #1
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();
                });
            }
        }
Exemple #2
0
        private static void GetPcIncrementFiles(Action <bool, System.Net.UploadDataCompletedEventArgs> completedCB, Action <long> progressCB)
        {
            var lst = from item in verInfo.FileInfos
                      select item.FileRelativePath;
            var obj = new
            {
                FileList = lst
            };

            System.Net.WebClient client = new System.Net.WebClient();
            client.UploadProgressChanged += (s, e) => progressCB?.Invoke(e.BytesReceived);
            client.UploadDataCompleted   += (s, e) =>
            {
                if (!e.Cancelled && e.Error == null)
                {
                    completedCB?.Invoke(true, e);
                    if (File.Exists("myzip.zip"))
                    {
                        File.Delete("myzip.zip");
                    }
                    File.WriteAllBytes("myzip.zip", e.Result);
                    using (ZipFile zip = new ZipFile("myzip.zip"))
                    {
                        zip.ExtractAll("temp", ExtractExistingFileAction.OverwriteSilently);
                    }
                    foreach (var item in verInfo.FileInfos)
                    {
                        try
                        {
                            var info = new FileInfo(Path.Combine("temp", item.FileRelativePath));
                            info.Replace(item.FileRelativePath, $"bak_{item.FileRelativePath}");
                        }
                        catch (Exception)
                        {
                        }
                    }
                    UpdateAppSetting(verInfo.LatestVersion.ToString());
                }
                else
                {
                    completedCB?.Invoke(false, e);
                }
            };

            client.Headers.Add(System.Net.HttpRequestHeader.ContentType, "application/json");
            client.UploadDataAsync(new Uri(PcIncrementFiles), Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(obj)));
        }
Exemple #3
0
        public void DownloadUpdateFile()
        {
            string url    = downUrl;
            var    client = new System.Net.WebClient();

            client.DownloadProgressChanged += (sender, e) =>
            {
                UpdateProcess(e.BytesReceived, e.TotalBytesToReceive);
            };
            client.UploadDataCompleted += (sender, e) =>
            {
                string       zipFilePath = System.IO.Path.Combine(updateFileDir, "update.zip");
                byte[]       data        = e.Result;
                BinaryWriter writer      = new BinaryWriter(new FileStream(zipFilePath, FileMode.OpenOrCreate));
                writer.Write(data);
                writer.Flush();
                writer.Close();

                System.Threading.ThreadPool.QueueUserWorkItem((s) =>
                {
                    Action f = () =>
                    {
                        txtProcess.Text = "开始更新程序...";
                    };
                    this.Dispatcher.Invoke(f);

                    string tempDir = System.IO.Path.Combine(updateFileDir, "temp");
                    if (!Directory.Exists(tempDir))
                    {
                        Directory.CreateDirectory(tempDir);
                    }
                    UnZipFile(zipFilePath, tempDir);

                    //移动文件
                    //App
                    if (Directory.Exists(System.IO.Path.Combine(tempDir, "App")))
                    {
                        CopyDirectory(System.IO.Path.Combine(tempDir, "App"), appDir);
                    }

                    f = () =>
                    {
                        txtProcess.Text = "更新完成!";

                        try
                        {
                            //清空缓存文件夹
                            string rootUpdateDir = updateFileDir.Substring(0, updateFileDir.LastIndexOf(System.IO.Path.DirectorySeparatorChar));
                            foreach (string p in System.IO.Directory.EnumerateDirectories(rootUpdateDir))
                            {
                                if (!p.ToLower().Equals(updateFileDir.ToLower()))
                                {
                                    System.IO.Directory.Delete(p, true);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            //MessageBox.Show(ex.Message);
                        }
                    };
                    this.Dispatcher.Invoke(f);

                    try
                    {
                        f = () =>
                        {
                            AlertWin alert = new AlertWin("更新完成,是否现在启动软件?")
                            {
                                WindowStartupLocation = WindowStartupLocation.CenterOwner, Owner = this
                            };
                            alert.Title   = "更新完成";
                            alert.Loaded += (ss, ee) =>
                            {
                                alert.YesButton.Width = 40;
                                alert.NoButton.Width  = 40;
                            };
                            alert.Width  = 300;
                            alert.Height = 200;
                            alert.ShowDialog();
                            if (alert.YesBtnSelected)
                            {
                                //启动软件
                                string exePath        = System.IO.Path.Combine(appDir, callExeName + ".exe");
                                var info              = new System.Diagnostics.ProcessStartInfo(exePath);
                                info.UseShellExecute  = true;
                                info.WorkingDirectory = appDir;// exePath.Substring(0, exePath.LastIndexOf(System.IO.Path.DirectorySeparatorChar));
                                System.Diagnostics.Process.Start(info);
                            }
                            else
                            {
                            }
                            this.Close();
                        };
                        this.Dispatcher.Invoke(f);
                    }
                    catch (Exception ex)
                    {
                        //MessageBox.Show(ex.Message);
                    }
                });
            };
            //MessageBox.Show(url);
            client.UploadDataAsync(new Uri(url), "post", new byte[0]);
        }