コード例 #1
0
        private void UpdateFiles(List <UpdateFileEntryYaml> updateFileEntries)
        {
            if (Cancelled)
            {
                return;
            }

            Label_ProgressBar1.SafeInvoke(() => Label_ProgressBar1.Text = Label_ProgressBar3.Text);

            try
            {
                PercentageProgressBar.SafeInvoke(delegate { PercentageProgressBar.Maximum = updateFileEntries.Count; PercentageProgressBar.Step = 1; PercentageProgressBar.Value = 0; });
                Parallel.ForEach(updateFileEntries, (updateFileEntry, state) =>
                {
                    if (Cancelled)
                    {
                        state.Stop();
                    }

                    var dlUri    = new Uri(DLUri, updateFileEntry.AbsoluteFilePath);
                    var tempFile = new TempFile(updateFileEntry.AbsoluteFilePath);
                    using (var downloader = new WebClient())
                    {
                        downloader.DownloadFile(dlUri, tempFile.Path);
                        PercentageProgressBar.SafeInvoke(() => PercentageProgressBar.PerformStep());
                    }
                });
            }
            catch (UriFormatException)          { DownloadErrorMessage(); return; }
            catch (UnauthorizedAccessException) { DownloadErrorMessage(); return; }
            catch (IOException)                 { DownloadErrorMessage(); return; }
            catch (WebException)                { DownloadErrorMessage(); return; }

            ReplaceFiles(updateFileEntries);
        }
コード例 #2
0
        private void DirectUpdaterForm_Shown(object sender, EventArgs args)
        {
            TempFile.Delete();

            try
            {
                using (Downloader = new WebClient())
                {
                    Downloader.DownloadFileCompleted += (s, e) =>
                    {
                        Label_ProgressBar1.SafeInvoke(() =>
                        {
                            PercentageProgressBar.Value = 0;
                            Label_ProgressBar1.Text     = Label_ProgressBar2.Text;
                        });

                        BackgroundWorker_Extractor.RunWorkerAsync();
                    };
                    Downloader.DownloadProgressChanged += (s, e) =>
                    {
                        this.SafeInvoke(delegate
                        {
                            double bytesIn              = e.BytesReceived;
                            double totalBytes           = e.TotalBytesToReceive;
                            double percentage           = bytesIn / totalBytes * 100;
                            PercentageProgressBar.Value = (int)percentage;
                        });
                    };
                    Downloader.DownloadFileAsync(new Uri(ReleaseAsset.BrowserDownloadUrl), TempFile.Path);
                }
            }
            catch (WebException) { }
        }
コード例 #3
0
        private List <UpdateFileEntryYaml> StartUpdate()
        {
            if (Cancelled)
            {
                return(new List <UpdateFileEntryYaml>());
            }

            Label_ProgressBar1.SafeInvoke(() => Label_ProgressBar1.Text = Label_ProgressBar2.Text);

            var newUpdateInfo = UpdateInfoYaml.Deserialize(NewUpdateInfoFile.ReadAllText());

            var crc32 = new Crc32();
            var sha1  = new SHA1Managed();
            var notValidFileEntries = new List <UpdateFileEntryYaml>();

            PercentageProgressBar.SafeInvoke(delegate { PercentageProgressBar.Maximum = newUpdateInfo.Count(); PercentageProgressBar.Step = 1; PercentageProgressBar.Value = 0; });
            Parallel.ForEach(newUpdateInfo, (updateFileEntry, state) =>
            {
                if (Cancelled)
                {
                    state.Stop();
                }

                PercentageProgressBar.SafeInvoke(() => PercentageProgressBar.PerformStep());

                if (UpdatedFolder.CheckExists(updateFileEntry.AbsoluteFilePath) != ExistenceCheckResult.FileExists)
                {
                    notValidFileEntries.Add(updateFileEntry);
                    return;
                }
                using (var fs = UpdatedFolder.GetFile(updateFileEntry.AbsoluteFilePath).Open(PCLExt.FileStorage.FileAccess.Read))
                {
                    var crc32Hash = string.Empty;
                    var sha1Hash  = string.Empty;
                    crc32Hash     = crc32.ComputeHash(fs).Aggregate(crc32Hash, (current, b) => current + b.ToString("x2").ToLower());
                    if (crc32Hash == updateFileEntry.CRC32)
                    {
                        sha1Hash = sha1.ComputeHash(fs).Aggregate(sha1Hash, (current, b) => current + b.ToString("x2").ToLower());
                        if (sha1Hash == updateFileEntry.SHA1)
                        {
                            return;
                        }
                        else
                        {
                            notValidFileEntries.Add(updateFileEntry);
                            return;
                        }
                    }
                    else
                    {
                        notValidFileEntries.Add(updateFileEntry);
                        return;
                    }
                }
            });

            return(notValidFileEntries);
        }