3

I know a few similar questions have been asked on how to download files using WebClient. I can download individual files perfectly fine, but I want to download a range of files. Anywhere from 1-6000 files. I can download them just fine into my current directory, but I am stumped on how to download them to a different directory based upon where they're being downloaded from. Do I need to temporarily change the current working directory just before downloading them?

And slightly on the same topic, I'm stuck on how to verify the files exist before downloading them. I don't want to waste bandwidth or diskspace with empty files.. Here's what I have so far:

            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);
                //}
            }
Jerry
  • 149
  • 3
  • 13

2 Answers2

1

Do I need to temporarily change the current working directory just before downloading them?

The second parameter can be a full path @"C:\folder\file.png". If you're fine with relative path to your current directory, just change the code to webClient.DownloadFileAsync(uri2, directoryName + partPBSSurl); or even better use System.Path.Combine(directoryName, partPBSSurl)

Sure you can know the size before If sever supports that. See: How to get the file size from http headers

I don't want to waste bandwidth or diskspace with empty files.

I wouldn't worry about that. The performance slow down is negligible.

Community
  • 1
  • 1
Lukasz Madon
  • 14,664
  • 14
  • 64
  • 108
0

There is no need to change the current directory. You are already using an overload of DownloadFileAsync that accepts a file path as the second parameter.

Just ensure that partPBSSurl contains a full path to the destination file, including both the directory and filename.

With regard to your second question of avoiding wasted time if the file does not exist, it so happens that I asked the same question recently:

Fail Fast with WebClient

Finally, I recently extended WebClient to provide simpler progress change events and allow for the timeout to be changed. I posed that code here:

https://stackoverflow.com/a/9763976/141172

Community
  • 1
  • 1
Eric J.
  • 147,927
  • 63
  • 340
  • 553