在C#中下载多个文件(一次只能下载一个/队列)
我正在用C#编写一个更新程序,现在需要一些帮助(我不熟悉这种语言,请解释一下你的提示:D) 我的代码:在C#中下载多个文件(一次只能下载一个/队列),c#,windows,file,download,C#,Windows,File,Download,我正在用C#编写一个更新程序,现在需要一些帮助(我不熟悉这种语言,请解释一下你的提示:D) 我的代码: System.IO.Directory.CreateDirectory("tmp"); int mmx = updnec.Count(); label6.Text = "0/" + mmx; MessageBox.Show(label6.Text); int mmn = 0;
System.IO.Directory.CreateDirectory("tmp");
int mmx = updnec.Count();
label6.Text = "0/" + mmx;
MessageBox.Show(label6.Text);
int mmn = 0;
foreach (string lz in link)
{
MessageBox.Show(lz);
client.DownloadFileAsync(new Uri(lz), "tmp/update00" + mmn + ".exe");
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
label6.Text = mmn + "/" + mmx;
mmn = +1;
//Whithout that while{} - part, it tries to download all links from the list at once. But I don't want the program to do that. I don't want it to show a message all the time, too, but when i leave that while{} - part empty, the program just freezes and doesn't even download.
while (progressBar1.Maximum != progressBar1.Value)
{
MessageBox.Show("Test");
}
}
}
void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressBar1.Maximum = (int)e.TotalBytesToReceive;
progressBar1.Value = (int)e.BytesReceived;
string sysload = progressBar1.Value /1000000 + "/" + progressBar1.Maximum /1000000 + " MB geladen";
label12.Text = sysload;
}
请阅读我的代码中的注释
如何解决我的问题!?请帮帮我
雷迪特克
编辑:
label11.Text=System.DateTime.Now.ToString();
backgroundWorker1.RunWorkerAsync(link);
}
私有void backgroundWorker1\u DoWork(对象发送方,DoWorkEventArgs e)
{
List lz=(List)e.参数;
int-mmn=0;
progressBar1.Style=ProgressBarStyle.Blocks;
System.IO.Directory.CreateDirectory(“tmp”);
int mmx=10;
label6.Text=“0/”+mmx;
foreach(lz中的字符串lkz)
使用(var client=new WebClient())
{
MessageBox.Show(“你好”+lkz);
client.DownloadFile(lkz,“tmp/update00”+mmn+“.exe”);
}
}
尝试使用BackgroundWorker,以同步方式而不是异步方式下载文件。
您可以在此处找到一个示例:
以下是一个例子:
void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
var links = new[] { "link1", "link2" };
var percentProgressStep = (100 / links.Length) + 1
using (var client = new WebClient())
foreach (string l in links)
{
client.DownloadFile(l, Path.GetTempFileName());
backgroundWorker1.ReportProgress(percentProgressStep);
}
}
您可以做的是将文件压缩到一个zip文件中,然后将zip文件发送到客户端。我不认为有其他方法可以做到这一点。查看下面的链接以了解更多信息为什么不包括一个问题而不是阅读评论的说明?寻求调试帮助的问题必须包含问题本身的特定问题。@MoezRebai这对我来说是不可能的:D@Sayse向您展示我的问题。@Comment部分(“/”部分)中的Sayse是我的问题。Quote:虽然{}-part,但它会尝试一次下载列表中的所有链接。但我不想让这个节目这么做。我不想让它一直显示消息,但是当我在{}-part为空时,程序就冻结了,甚至不下载。我会试试看。谢谢你的回答:)你同步下载文件是什么意思BackgroundWorker已经在异步(不阻止您的表单)中为您工作了。请用一段代码告诉我您的意思,好吗?:)void backgroundWorker1_DoWork(对象发送方,doworkereventargs e){var links=new[]{“link1”,“link2”};使用(var client=new WebClient())foreach(links中的字符串l){client.DownloadFile(l,Path.GetTempFileName());}
void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
var links = new[] { "link1", "link2" };
var percentProgressStep = (100 / links.Length) + 1
using (var client = new WebClient())
foreach (string l in links)
{
client.DownloadFile(l, Path.GetTempFileName());
backgroundWorker1.ReportProgress(percentProgressStep);
}
}