private static async Task DownloadFile(string uri, string fileName, string mod, DateTime updateTime, 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) { _logger.WriteLine($"Couldn't delete the already existing {assemblyLocation}/Downloads/{fileName} ({e.Message})", LoggerType.Error); return; } } progressBox = new ProgressBox(cancellationToken); progressBox.progressBar.Value = 0; progressBox.finished = false; progressBox.Title = $"Download Progress"; progressBox.Show(); progressBox.Activate(); // Write and download the file using (var fs = new FileStream( $@"{assemblyLocation}/Downloads/{fileName}", FileMode.Create, FileAccess.Write, FileShare.None)) { var client = new HttpClient(); await client.DownloadAsync(uri, fs, fileName, progress, cancellationToken.Token); } progressBox.Close(); await ExtractFile(fileName, mod, updateTime); } 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(); } return; } catch (Exception e) { if (progressBox != null) { progressBox.finished = true; progressBox.Close(); } _logger.WriteLine($"Error whilst downloading {fileName} ({e.Message})", LoggerType.Error); } }
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 ProgressBox(cancellationToken); progressBox.progressBar.Value = 0; progressBox.finished = false; progressBox.Title = $"Download 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(); } MessageBox.Show($"Error whilst downloading {fileName}: {e.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Warning); cancelled = true; } }
private static async Task DownloadFileDaddy(string uri, string fileName, string version, Progress <DownloadProgress> progress, CancellationTokenSource cancellationToken) { try { // Create the downloads folder if necessary if (!Directory.Exists(@$ "{assemblyLocation}/Downloads")) { Directory.CreateDirectory(@$ "{assemblyLocation}/Downloads"); } // Create the downloads folder if necessary if (!Directory.Exists(@$ "{assemblyLocation}/Downloads/FileDaddyUpdate")) { Directory.CreateDirectory(@$ "{assemblyLocation}/Downloads/FileDaddyUpdate"); } progressBox = new ProgressBox(cancellationToken); progressBox.progressBar.Value = 0; progressBox.progressText.Text = $"Downloading {fileName}"; progressBox.Title = "FileDaddy Update Progress"; progressBox.finished = false; progressBox.Show(); progressBox.Activate(); // Write and download the file using (var fs = new FileStream( $@"{assemblyLocation}/Downloads/FileDaddyUpdate/{fileName}", FileMode.Create, FileAccess.Write, FileShare.None)) { await client.DownloadAsync(uri, fs, fileName, progress, cancellationToken.Token); } // Rename the file if (!File.Exists($@"{assemblyLocation}/Downloads/FileDaddyUpdate/{version}.7z")) { File.Move($@"{assemblyLocation}/Downloads/FileDaddyUpdate/{fileName}", $@"{assemblyLocation}/Downloads/FileDaddyUpdate/{version}.7z"); } progressBox.Close(); } catch (OperationCanceledException) { // Remove the file is it will be a partially downloaded one and close up File.Delete(@$ "{assemblyLocation}/Downloads/FileDaddyUpdate/{fileName}"); if (progressBox != null) { progressBox.finished = true; progressBox.Close(); } return; } catch (Exception e) { Console.WriteLine($"[ERROR] Error whilst downloading {fileName}: {e.Message}"); if (progressBox != null) { progressBox.finished = true; progressBox.Close(); } } }