我知道关于如何使用 WebClient 下载文件,有人问过一些类似的问题。我可以很好地下载单个文件,但我想下载一系列文件。1-6000 个文件。我可以将它们很好地下载到我当前的目录中,但是我很难根据下载它们的位置将它们下载到不同的目录。我需要在下载它们之前临时更改当前工作目录吗?
稍微在同一主题上,我被困在如何在下载文件之前验证文件是否存在。我不想用空文件浪费带宽或磁盘空间。这是我目前所拥有的:
for (int x = 1; x <= 6000; x++)
{
pbsscount = x.ToString();
// Used for downloading file
string directoryName = textBox1.Text.ToString().Replace(":", "_");
if (!Directory.Exists(textBox1.Text))
Directory.CreateDirectory(directoryName.Substring(7));
string wholePBSSurl = textBox1.Text + "/" + "pb" + pbsscount.PadLeft(6, '0') + ".png";
// Used for saving file, file name in directory
string partPBSSurl = "pb" + pbsscount.PadLeft(6, '0') + ".png";
Uri uri2 = new Uri(wholePBSSurl);
//if (fileExists(wholePBSSurl))
//{
// Initialize downloading info, grab progressbar info
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
// Save file to folder
//webClient.DownloadFileAsync(uri2, textBox1.Text + "/" + partPBSSurl);
webClient.DownloadFileAsync(uri2, partPBSSurl);
//}
}