public void DownloadUpdateFile()
        {
            var url = Constants.RemoteUrl + "/app.zip";

            var client = new System.Net.WebClient();

            client.DownloadProgressChanged += (sender, e) =>
            {
                UpdateProcess(e.BytesReceived, e.TotalBytesToReceive);
            };
            client.DownloadDataCompleted += (sender, e) =>
            {
                MessageBox.Show(updateFileDir);
                string       zipFilePath = System.IO.Path.Combine(updateFileDir, "app.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);
                    }
                });
            };
            client.DownloadDataAsync(new Uri(url));
        }
Exemple #2
0
        public void DownloadUpdateFile()
        {
            string url = "";

            if (!string.IsNullOrWhiteSpace(updateInfo.DownloadUrl))
            {
                url = updateInfo.DownloadUrl;
            }

            //string url = Constants.RemoteUrl + callExeName + "/update.zip";
            url = url + "/" + callExeName + "/" + updateInfo.PackageName;

            LogerManager.Current.AsyncDebug("下载升级包:" + url);

            var client = new System.Net.WebClient();

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

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

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

                    //移动文件
                    //App
                    var newAppDir = Path.Combine(tempDir, "app");
                    if (Directory.Exists(newAppDir))
                    {
                        //启动升级前脚本
                        if (!string.IsNullOrWhiteSpace(updateInfo.FileExecuteBefore))
                        {
                            var scriptPath = Path.Combine(newAppDir, updateInfo.FileExecuteBefore + ".exe");
                            StartApp(scriptPath, updateInfo.ExecuteArgumentBefore);
                        }


                        // 备份 将当前版本的目录文件复制到bak
                        CopyDirectory(appDir, Path.Combine(updateFileDir, "bak"));
                        // 升级 将app文件夹中的内容复制到程序运行目标
                        try
                        {
                            //启动升级后脚本
                            if (!string.IsNullOrWhiteSpace(updateInfo.FileExecuteAfter))
                            {
                                var scriptPath = Path.Combine(newAppDir, updateInfo.FileExecuteAfter + ".exe");
                                StartApp(scriptPath, updateInfo.ExecuteArgumentAfter);
                            }

                            CopyDirectory(newAppDir, appDir);
                        }
                        catch (Exception ex)
                        {
                            LogerManager.Current.AsyncError("更新错误:" + ex.Message);
                            // 失败回滚
                            CopyDirectory(Path.Combine(updateFileDir, "bak"), appDir);
                            return;
                        }
                    }

                    f = () =>
                    {
                        TxtProcess.Text = "更新完成!";
                        //TxtProcess.Text = "Update completed";

                        try
                        {
                            //清空缓存文件夹
                            string rootUpdateDir = updateFileDir.Substring(0,
                                                                           updateFileDir.LastIndexOf(Path.DirectorySeparatorChar));
                            foreach (string p in Directory.EnumerateDirectories(rootUpdateDir))
                            {
                                if (!p.ToLower().Equals(updateFileDir.ToLower()))
                                {
                                    Directory.Delete(p, true);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            LogerManager.Current.AsyncError("更新后清空缓存文件错误:" + ex.Message);
                            //MessageBox.Show(ex.Message);
                        }
                        finally
                        {
                            YesButton.IsEnabled = true;
                            NoButton.IsEnabled  = true;
                        }
                    };
                    this.Dispatcher.Invoke(f);

                    try
                    {
                        f = () =>
                        {
                            AlertWin alert = new AlertWin("更新完成,是否启动软件?")
                            {
                                WindowStartupLocation = WindowStartupLocation.CenterOwner, Owner = this
                            };
                            alert.Title = "更新完成";
                            //AlertWin alert = new AlertWin("The application has been updated to start it?") { WindowStartupLocation = WindowStartupLocation.CenterOwner, Owner = this };

                            //alert.Title = "Update completed";
                            alert.Loaded += (ss, ee) =>
                            {
                                alert.YesButton.Width = 50;
                                alert.NoButton.Width  = 50;
                            };
                            alert.Width  = 300;
                            alert.Height = 200;
                            alert.ShowDialog();
                            if (alert.YesBtnSelected)
                            {
                                //启动软件
                                string exePath = Path.Combine(appDir, callExeName + ".exe");
                                StartApp(exePath);
                            }
                            this.Close();
                        };
                        this.Dispatcher.Invoke(f);
                    }
                    catch (Exception ex)
                    {
                        LogerManager.Current.AsyncError("下载更新错误:" + ex.Message);
                        //MessageBox.Show(ex.Message);
                    }
                });
            };
            client.DownloadDataAsync(new Uri(url));
        }