/// <summary>
        ///
        /// </summary>
        /// <param name="savePath"></param>
        /// <param name="url"></param>
        /// <param name="overwrite"></param>
        /// <returns></returns>
        private bool DownloadFile(string savePath, string url, bool overwrite = true)
        {
            //打开上次下载的文件
            long SPosition = 0;
            var  exist     = File.Exists(savePath) && overwrite == false;

            //实例化流对象
            using (var FStream = exist ? File.OpenWrite(savePath) : new FileStream(savePath, FileMode.Create))
            {
                if (exist)
                {
                    SPosition = FStream.Length;
                    FStream.Seek(SPosition, SeekOrigin.Current);
                }

                try
                {
                    //打开网络连接
                    var myRequest = (HttpWebRequest)HttpWebRequest.Create(url);
                    if (SPosition > 0)
                    {
                        myRequest.AddRange((int)SPosition);
                    }

                    long totalSize = SPosition;

                    var args = new UpdateProgressChangedEventArgs {
                        CurrentProgress = _progressValue, TotalProgress = _totalProgress, Message = "下载文件中...", State = UpdateStateChangeStateEnum.InProgress
                    };

                    using (var myStream = myRequest.GetResponse().GetResponseStream())
                    {
                        //定义一个字节数据
                        var btContent = new byte[512];
                        var intSize   = myStream.Read(btContent, 0, 512);
                        while (intSize > 0)
                        {
                            totalSize           += intSize;
                            args.CurrentProgress = _progressValue + totalSize;
                            OnUpdateProgressChangedEvent(this, args);
                            if (args.NeedCancel)
                            {
                                _stopSignal = true;
                                return(false);
                            }


                            FStream.Write(btContent, 0, intSize);
                            intSize = myStream.Read(btContent, 0, 512);
                        }
                    }

                    return(true);
                }
                catch (Exception)
                {
                    return(false);
                }
            }
        }
        private void UpdateProgress(UpdateProgressChangedEventArgs e)
        {
            if (lblMessage.InvokeRequired)
            {
                Invoke(new UpdateProgressDelegate(UpdateProgress), e);
            }
            else
            {
                if (lblMessage.Text != e.Message)
                {
                    lblMessage.Text = e.Message;
                }

                var progress = (int)((double)e.CurrentProgress / e.TotalProgress * 100);
                if (progress != progressBar1.Value && progress <= 100)
                {
                    progressBar1.Value = progress;
                }
                if (_stopSignal)
                {
                    e.NeedCancel = true;
                }

                if (e.State == UpdateStateChangeStateEnum.Done)
                {
                    btnUpdate.Text    = "开始更新";
                    btnUpdate.Enabled = false;
                    if (MessageBox.Show("更新完成,是否立即打开软件?", "完成", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        var startInfo = new ProcessStartInfo
                        {
                            UseShellExecute  = true,
                            WorkingDirectory = Environment.CurrentDirectory,
                            FileName         = "LoowooTech.Stock.exe",
                        };
                        try
                        {
                            Process.Start(startInfo);
                            Application.Exit();
                        }
                        catch
                        {
                            return;
                        }
                    }
                }
                else if (e.State == UpdateStateChangeStateEnum.Fail)
                {
                    btnUpdate.Text    = "开始更新";
                    btnUpdate.Enabled = true;
                }
            }
        }
        public void StartUpdate()
        {
            _stopSignal = false;
            var localFiles = new List <string>();

            _progressValue = 0;
            _totalProgress = (long)(_totalProgress * 1.1);

            OnUpdateProgressChangedEvent(this, new UpdateProgressChangedEventArgs {
                CurrentProgress = _progressValue, TotalProgress = _totalProgress, Message = "开始更新", State = UpdateStateChangeStateEnum.InProgress
            });


            foreach (var file in RemoteVersion.Files)
            {
                for (var i = 0; i < 3; i++)
                {
                    try
                    {
                        var localFile = DownloadFile(file);
                        if (string.IsNullOrEmpty(localFile))
                        {
                            throw new Exception("下载失败");
                        }
                        _progressValue += file.Size;
                        var args = new UpdateProgressChangedEventArgs {
                            CurrentProgress = _progressValue, TotalProgress = _totalProgress, Message = "下载文件中...", State = UpdateStateChangeStateEnum.InProgress
                        };
                        OnUpdateProgressChangedEvent(this, args);
                        if (args.NeedCancel)
                        {
                            return;
                        }

                        localFiles.Add(localFile);
                        break;
                    }
                    catch
                    {
                        if (_stopSignal == true)
                        {
                            return;
                        }
                        if (i == 2)
                        {
                            OnUpdateProgressChangedEvent(this, new UpdateProgressChangedEventArgs {
                                CurrentProgress = 0, TotalProgress = 100, Message = "下载远程文件失败", State = UpdateStateChangeStateEnum.Fail
                            });
                            return;
                        }
                        else
                        {
                            OnUpdateProgressChangedEvent(this, new UpdateProgressChangedEventArgs {
                                CurrentProgress = 0, TotalProgress = 100, Message = "下载失败正在重试", State = UpdateStateChangeStateEnum.InProgress
                            });
                        }
                    }
                }
            }

            OnUpdateProgressChangedEvent(this, new UpdateProgressChangedEventArgs {
                CurrentProgress = _progressValue, TotalProgress = _totalProgress, Message = "正在更新文件...", State = UpdateStateChangeStateEnum.InProgress
            });
            for (var i = 0; i < localFiles.Count; i++)
            {
                var file = RemoteVersion.Files[i];
                if (file.Type == "Zip")
                {
                    ExtractFile(file, localFiles[i]);
                }
                else
                {
                    ReplaceFile(file, localFiles[i]);
                }
            }

            using (var writer = new StreamWriter("local.json", false))
            {
                writer.WriteLine(JsonConvert.SerializeObject(RemoteVersion));
            }
            LocalVersion = RemoteVersion;

            OnUpdateProgressChangedEvent(this, new UpdateProgressChangedEventArgs {
                CurrentProgress = _totalProgress, TotalProgress = _totalProgress, Message = "自动更新完成", State = UpdateStateChangeStateEnum.Done
            });
        }
 private void UpdateManager_OnUpdateProgressChangedEvent(object sender, UpdateProgressChangedEventArgs e)
 {
     UpdateProgress(e);
 }