简体   繁体   English

C#下载一系列文件

[英]C# Downloading a range of files

I know a few similar questions have been asked on how to download files using WebClient. 我知道有一些关于如何使用WebClient下载文件的类似问题。 I can download individual files perfectly fine, but I want to download a range of files. 我可以完全下载单个文件,但我想下载一系列文件。 Anywhere from 1-6000 files. 1-6000个文件中的任何位置。 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);
                //}
            }

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" . 第二个参数可以是完整路径@"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); 如果您对当前目录的相对路径没问题,只需将代码更改为webClient.DownloadFileAsync(uri2, directoryName + partPBSSurl); or even better use System.Path.Combine(directoryName, partPBSSurl) 甚至更好地使用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 请参阅: 如何从http标头获取文件大小

I don't want to waste bandwidth or diskspace with empty files. 我不想浪费空文件带宽或磁盘空间。

I wouldn't worry about that. 我不担心。 The performance slow down is negligible. 性能下降可以忽略不计。

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. 您已经使用了DownloadFileAsync的重载,它接受文件路径作为第二个参数。

Just ensure that partPBSSurl contains a full path to the destination file, including both the directory and filename. 只需确保partPBSSurl包含目标文件的完整路径,包括目录和文件名。

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 使用WebClient快速失败

Finally, I recently extended WebClient to provide simpler progress change events and allow for the timeout to be changed. 最后,我最近扩展了WebClient以提供更简单的进度更改事件,并允许更改超时。 I posed that code here: 我在这里提出了代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM