Esempio n. 1
0
 private async Task DownloadFile(string uri, string fileName, Progress <DownloadProgress> progress, CancellationTokenSource cancellationToken)
 {
     try
     {
         // Create the downloads folder if necessary
         Directory.CreateDirectory($@"{assemblyLocation}/Downloads");
         // Download the file if it doesn't already exist
         if (File.Exists($@"{assemblyLocation}/Downloads/{fileName}"))
         {
             try
             {
                 File.Delete($@"{assemblyLocation}/Downloads/{fileName}");
             }
             catch (Exception e)
             {
                 MessageBox.Show($"Couldn't delete the already existing {assemblyLocation}/Downloads/{fileName} ({e.Message})",
                                 "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
                 return;
             }
         }
         progressBox = new UpdateProgressBox(cancellationToken);
         progressBox.progressBar.Value = 0;
         progressBox.finished          = false;
         progressBox.Title             = $"Update Progress";
         progressBox.Show();
         progressBox.Activate();
         // Write and download the file
         using (var fs = new FileStream(
                    $@"{assemblyLocation}/Downloads/{fileName}", FileMode.Create, FileAccess.Write, FileShare.None))
         {
             await client.DownloadAsync(uri, fs, fileName, progress, cancellationToken.Token);
         }
         progressBox.Close();
     }
     catch (OperationCanceledException)
     {
         // Remove the file is it will be a partially downloaded one and close up
         File.Delete($@"{assemblyLocation}/Downloads/{fileName}");
         if (progressBox != null)
         {
             progressBox.finished = true;
             progressBox.Close();
             cancelled = true;
         }
         return;
     }
     catch (Exception e)
     {
         if (progressBox != null)
         {
             progressBox.finished = true;
             progressBox.Close();
             cancelled = true;
         }
         MessageBox.Show($"Error whilst downloading {fileName}: {e.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Warning);
     }
 }
Esempio n. 2
0
 private async Task DownloadFile(string uri, string fileName, string game, DisplayedMetadata row, string version, Progress <DownloadProgress> progress, CancellationTokenSource cancellationToken, GameBananaItemUpdate update = null)
 {
     try
     {
         // Create the downloads folder if necessary
         if (!Directory.Exists($@"{assemblyLocation}\Downloads"))
         {
             Directory.CreateDirectory($@"{assemblyLocation}\Downloads");
         }
         // Download the file if it doesn't already exist
         if (!FileIOWrapper.Exists($@"{assemblyLocation}\Downloads\{fileName}"))
         {
             progressBox = new UpdateProgressBox(cancellationToken);
             progressBox.progressBar.Value = 0;
             progressBox.progressText.Text = $"Downloading {fileName}";
             progressBox.finished          = false;
             progressBox.Title             = $"{row.name} Update Progress";
             progressBox.Show();
             progressBox.Activate();
             Console.WriteLine($"[INFO] Downloading {fileName}");
             // Write and download the file
             using (var fs = new FileStream(
                        $@"{assemblyLocation}\Downloads\{fileName}", System.IO.FileMode.Create, FileAccess.Write, FileShare.None))
             {
                 await client.DownloadAsync(uri, fs, fileName, progress, cancellationToken.Token);
             }
             Console.WriteLine($"[INFO] Finished downloading {fileName}");
             progressBox.Close();
         }
         else
         {
             Console.WriteLine($"[INFO] {fileName} already exists in downloads, using this instead");
         }
         ExtractFile(fileName, game, row, version, update);
     }
     catch (OperationCanceledException)
     {
         // Remove the file is it will be a partially downloaded one and close up
         FileIOWrapper.Delete(@$ "Downloads\{fileName}");
         if (progressBox != null)
         {
             progressBox.finished = true;
             progressBox.Close();
         }
         return;
     }