我想用WebClient列出文件下载 但WebClient一次不允许多个动作,这段代码一次调用所有下载,但我希望单独下载文件
modlist.txt
mod1.jar
mod2.jar
mod3.jar
mod4.jar
代码
private void DownloadingFiles()
{
if (!File.Exists(dir + "modlist.txt"))
{
client.DownloadFileAsync(new Uri(site + "modlist.txt"), dir + "modlist.txt");
}
}
private void DownloadingMods()
{
var files = File.ReadAllLines(dir + "modlist.txt");
foreach (var filename in files)
{
if (!File.Exists(dir + "mods/" + filename))
{
client.DownloadFileAsync(new Uri(site + "mods/" + filename), dir + "mods/" + filename);
}
}
}
答案 0 :(得分:0)
您应该使用await
和async
之类的:
private async Task DownloadingFiles()
{
if (!File.Exists(dir + "modlist.txt"))
{
await client.DownloadFileTaskAsync(new Uri(site + "modlist.txt"), dir + "modlist.txt");
}
}
private async Task DownloadingMods()
{
var files = File.ReadAllLines(dir + "modlist.txt");
foreach (var filename in files)
{
if (!File.Exists(dir + "mods/" + filename))
{
await client.DownloadFileTaskAsync(new Uri(site + "mods/" + filename), dir + "mods/" + filename);
}
}
}