Ejemplo n.º 1
0
        public static void Download(
            string fileName,
            string version,
            Action <int> progressChanged,
            Action <bool, string, string> downloadComplete,
            out Action cancel)
        {
            string saveFileFullName = Path.Combine(SpecialPath.DownloadDirFullName, App.AppType.ToString() + version);

            progressChanged?.Invoke(0);
            using (var webClient = VirtualRoot.CreateWebClient()) {
                cancel = () => {
                    webClient.CancelAsync();
                };
                webClient.DownloadProgressChanged += (object sender, DownloadProgressChangedEventArgs e) => {
                    progressChanged?.Invoke(e.ProgressPercentage);
                };
                webClient.DownloadFileCompleted += (sender, e) => {
                    bool   isSuccess = !e.Cancelled && e.Error == null;
                    string message   = "下载成功";
                    if (e.Error != null)
                    {
                        message = "下载失败";
                        VirtualRoot.Out.ShowError(e.Error.Message);
                    }
                    if (e.Cancelled)
                    {
                        message = "已取消";
                    }
                    if (isSuccess)
                    {
                        // nothing need todo
                    }
                    else
                    {
                        VirtualRoot.Out.ShowError(message, 4);
                    }
                    downloadComplete?.Invoke(isSuccess, message, saveFileFullName);
                };
                OfficialServer.FileUrlService.GetNTMinerUrlAsync(fileName, (url, e) => {
                    webClient.DownloadFileAsync(new Uri(url), saveFileFullName);
                });
            }
        }