Как загрузить несколько файлов с помощью downloadfileasync C#?
Привет, я использую этот код для своей работы.
но этот код не загружает несколько файлов с помощью DownloadFileAsync
мой код таков
<pre>using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Diagnostics; using System.Threading; namespace Ping_Service { public partial class Form1 : Form { private readonly string Version = "1.1"; public Form1() { InitializeComponent(); } WebClient client; public string getVersion() { return Version; } //Method to Update private void checkForUpdate() { string URL = "https://tweak-team.ir/"; string AppName = "PingService.exe"; string ServerVersion; string serverVersionName = "pingservice.txt"; // i will make take a old app to check if its work :) WebRequest req = WebRequest.Create(URL + serverVersionName); WebResponse res = req.GetResponse(); Stream str = res.GetResponseStream(); StreamReader tr = new StreamReader(str); ServerVersion = tr.ReadLine(); if (getVersion() != ServerVersion) { //Update WebClient client = new WebClient(); byte[] appdata = client.DownloadData(URL + AppName); MessageBox.Show("Update is Available!", "PingService", MessageBoxButtons.OK, MessageBoxIcon.Warning); if (saveFileDialog1.ShowDialog() == DialogResult.OK) { using (FileStream fs = File.Create(saveFileDialog1.FileName)) { fs.Write(appdata, 0, appdata.Length); } } } else { MessageBox.Show("No Update is Available!", "PingService", MessageBoxButtons.OK, MessageBoxIcon.Information); } } private void Form1_Load(object sender, EventArgs e) { checkForUpdate(); } private void bunifuThinButton21_Click(object sender, EventArgs e) { string[] filePaths = Directory.GetFiles(@"C:\test"); foreach (string filePath in filePaths) File.Delete(filePath); Thread thread = new Thread(() => { WebClient client = new WebClient(); client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged); client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted); client.DownloadFileAsync(new Uri("https://pingservice.ir/icon/Server/pingservice-Germany.ovpn"), @"C:\test\pingservice-Germany.ovpn"); client.DownloadFileAsync(new Uri("https://pingservice.ir/icon/Server/pingservice-Netherland.ovpn"), @"C:\test\pingservice-Netherland.ovpn"); client.DownloadFileAsync(new Uri("https://pingservice.ir/icon/Server/Ps-Germany-New.ovpn"), @"C:\test\C:\test\Ps-Germany-New.ovpn"); }); thread.Start(); } void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) { this.BeginInvoke((MethodInvoker)delegate { double bytesIn = double.Parse(e.BytesReceived.ToString()); double totalBytes = double.Parse(e.TotalBytesToReceive.ToString()); double percentage = bytesIn / totalBytes * 100; progressBar1.Value = int.Parse(Math.Truncate(percentage).ToString()); }); } void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) { this.BeginInvoke((MethodInvoker)delegate { MessageBox.Show("Download Compelte", "PingService"); }); } } }
спасибо за помощь
Что я уже пробовал:
я должен загрузить эти файлы, и мне нужен этот индикатор выполнения.