Created
October 9, 2025 12:39
-
-
Save sunmeat/13f85279baa431e5b36dcda94ed189ab to your computer and use it in GitHub Desktop.
files & web
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Net; | |
using System.Text; | |
namespace ConsoleEvents | |
{ | |
public class FileDownloader | |
{ | |
public event EventHandler<double>? ProgressChanged; | |
public event EventHandler<string>? DownloadCompleted; | |
public void DownloadFile(string url, string path) | |
{ | |
var request = (HttpWebRequest)WebRequest.Create(url); | |
using var response = (HttpWebResponse)request.GetResponse(); | |
long totalBytes = response.ContentLength; | |
using var stream = response.GetResponseStream(); | |
using var fileStream = new FileStream(path, FileMode.Create); | |
var buffer = new byte[8192]; | |
long totalRead = 0; | |
int read; | |
while ((read = stream.Read(buffer, 0, buffer.Length)) > 0) | |
{ | |
fileStream.Write(buffer, 0, read); | |
totalRead += read; | |
if (totalBytes > 0) | |
{ | |
double progress = (totalRead / (double)totalBytes) * 100; | |
ProgressChanged?.Invoke(this, progress); | |
} | |
} | |
DownloadCompleted?.Invoke(this, path); | |
} | |
} | |
public class FileCopier | |
{ | |
public event EventHandler<double>? ProgressChanged; | |
public event EventHandler<string>? CopyCompleted; | |
public void CopyFile(string source, string dest) | |
{ | |
if (!File.Exists(source)) | |
{ | |
File.WriteAllText(source, new string('A', 100000)); | |
} | |
long totalBytes = new FileInfo(source).Length; | |
using var srcStream = File.OpenRead(source); | |
using var dstStream = File.Create(dest); | |
var buffer = new byte[8192]; | |
long totalRead = 0; | |
int read; | |
while ((read = srcStream.Read(buffer, 0, buffer.Length)) > 0) | |
{ | |
dstStream.Write(buffer, 0, read); | |
totalRead += read; | |
double progress = (totalRead / (double)totalBytes) * 100; | |
ProgressChanged?.Invoke(this, progress); | |
} | |
CopyCompleted?.Invoke(this, dest); | |
} | |
} | |
class Program | |
{ | |
static void Main() | |
{ | |
Console.OutputEncoding = Encoding.UTF8; | |
var downloader = new FileDownloader(); | |
var copier = new FileCopier(); | |
downloader.ProgressChanged += (sender, progress) => | |
Console.Write($"\rЗавантаження: {progress:F1}%"); | |
downloader.DownloadCompleted += (sender, path) => | |
Console.WriteLine($"\nЗавантаження завершено! Файл: {path}"); | |
copier.ProgressChanged += (sender, progress) => | |
Console.Write($"\rКопіювання: {progress:F1}%"); | |
copier.CopyCompleted += (sender, path) => | |
Console.WriteLine($"\nКопіювання завершено! Файл: {path}"); | |
downloader.DownloadFile("https://www.polly.com.ua/test/1Gb", "downloaded.txt"); | |
copier.CopyFile("downloaded.txt", "copy.txt"); | |
Console.ReadKey(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment