-
-
Save ebubekirbastama/e12ca392656fcd8be7ba0ab8366410c0 to your computer and use it in GitHub Desktop.
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; | |
using System.ComponentModel; | |
using System.IO; | |
using System.Net; | |
using System.Security.Cryptography; | |
using System.Threading.Tasks; | |
using System.Windows.Forms; | |
namespace Csharp_ile_ProgressBar_Kullanımı | |
{ | |
public partial class Form1 : Form | |
{ | |
private Downloader downloader; | |
public Form1() | |
{ | |
InitializeComponent(); | |
downloader = new Downloader(progressBar1); | |
} | |
private async void button1_Click(object sender, EventArgs e) | |
{ | |
await downloader.DownloadFileAsync(); | |
} | |
} | |
public class Downloader | |
{ | |
private ProgressBar progressBar; | |
private string fileUrl = "https://www.rarlab.com/rar/winrar-x64-624tr.exe"; | |
private string dosyaAdi = "winrar.exe"; | |
private string expectedHash = "d2e34475e926ea0b652d01bd629a7fe7"; // Örnek doğru hash | |
public Downloader(ProgressBar progressBar) | |
{ | |
this.progressBar = progressBar; | |
this.progressBar.Minimum = 0; | |
this.progressBar.Maximum = 100; | |
this.progressBar.Value = 0; | |
} | |
public async Task DownloadFileAsync() | |
{ | |
using (WebClient client = new WebClient()) | |
{ | |
client.DownloadProgressChanged += Client_DownloadProgressChanged; | |
client.DownloadFileCompleted += Client_DownloadFileCompleted; | |
await client.DownloadFileTaskAsync(new Uri(fileUrl), dosyaAdi); | |
} | |
} | |
private void Client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) | |
{ | |
progressBar.Invoke((MethodInvoker)delegate | |
{ | |
progressBar.Value = e.ProgressPercentage; | |
}); | |
} | |
private void Client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) | |
{ | |
if (e.Error == null) | |
{ | |
if (VerifyFileIntegrity(dosyaAdi, expectedHash)) | |
{ | |
MessageBox.Show("Dosya indirme tamamlandı ve bütünlük doğrulandı!"); | |
} | |
else | |
{ | |
MessageBox.Show("Dosya bütünlüğü doğrulanamadı. İndirilen dosya güvenli değil!"); | |
} | |
} | |
else | |
{ | |
MessageBox.Show("Dosya indirme başarısız oldu. Hata: " + e.Error.Message); | |
} | |
} | |
private bool VerifyFileIntegrity(string filePath, string expectedHash) | |
{ | |
using (FileStream fileStream = File.OpenRead(filePath)) | |
{ | |
using (MD5 md5 = MD5.Create()) | |
{ | |
byte[] hashBytes = md5.ComputeHash(fileStream); | |
string actualHash = BitConverter.ToString(hashBytes).Replace("-", "").ToLower(); | |
return string.Equals(expectedHash, actualHash, StringComparison.OrdinalIgnoreCase); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment