public partial class Form1 : Form
{
public class Filex
{
public string fileName { get; set; }
public string DownLoadString { get; set; }
public bool Downloaded { get; set; }
}
public Form1()
{
InitializeComponent();
}
WebClient DownloadClient = new WebClient();
List<Filex> FilesList = new List<Filex>();
public void StartDownload()
{
Filex FileName1 = new Filex();
FileName1.fileName = "AwesomeFile1.rar";
FileName1.DownLoadString = "http://download.thinkbroadband.com/5MB.zip";
Filex FileName2 = new Filex();
FileName2.fileName = "AwesomeFile2.rar";
FileName2.DownLoadString = "http://download.thinkbroadband.com/5MB.zip";
FilesList.Add(FileName1);
FilesList.Add(FileName2);
foreach (var File in FilesList)
{
DownloadFile(File.fileName, File.DownLoadString);
}
}
public void DownloadFile(string FileName, string FileURL)
{
string ApplicationPath = Path.GetDirectoryName(Application.ExecutablePath);
string DownloadLocation = ApplicationPath + "\\TempFiles";
bool exists = System.IO.Directory.Exists(DownloadLocation);
if (!exists)
{
System.IO.Directory.CreateDirectory(DownloadLocation);
}
DownloadClient = new WebClient();
Uri FileUriLink = new Uri(FileURL);
DownloadClient.DownloadProgressChanged += DownloadClient_DownloadProgressChanged;
DownloadClient.DownloadFileAsync(FileUriLink, DownloadLocation + FileName);
}
private void DownloadClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
string MiaxSize = GetFileSize(e.TotalBytesToReceive);
string MinSize = GetFileSize(e.BytesReceived);
FileSize_lbl.Text = MiaxSize + "/" + MinSize;
progressBar1.Maximum = ((int)e.TotalBytesToReceive / 100);
progressBar1.Value = ((int)e.BytesReceived / 100);
if (progressBar1.Value == progressBar1.Maximum)
{
UpdateState_lbl.Text = "Complete...";
}
else
{
UpdateState_lbl.Text = "Downloading...";
}
}
private string GetFileSize(double byteCount)
{
string size = "0 Bytes";
if (byteCount >= 1073741824.0)
size = String.Format("{0:##.##}", byteCount / 1073741824.0) + " GB";
else if (byteCount >= 1048576.0)
size = String.Format("{0:##.##}", byteCount / 1048576.0) + " MB";
else if (byteCount >= 1024.0)
size = String.Format("{0:##.##}", byteCount / 1024.0) + " KB";
else if (byteCount > 0 && byteCount < 1024.0)
size = byteCount.ToString() + " Bytes";
return size;
}
private void button1_Click(object sender, EventArgs e)
{
StartDownload();
}
}
大家好,当前代码完成工作但不是我想要的方式,它同时下载两个文件并同时报告进度条... 我想把它作为一个队列并一个接一个地分别报告每个文件
尝试了后台工作人员,但这让它变得更加可怕,因为它要求代表更改nececerry标签,文件名...大小... 1,多少文件等...
任何人都可以帮助您找到合适的文件下载解决方案