C# 在同一时间下载多个文件

C# 在同一时间下载多个文件,c#,asp.net,winforms,download,C#,Asp.net,Winforms,Download,我的列表框中有很多这样的链接:我想同时下载它们,有什么建议吗? 这段代码我用来下载单个文件 private void txtUrl_TextChanged(object sender, EventArgs e) { try { // function that enter the file name automatically in the savefiledialog. Uri uri = new Ur

我的列表框中有很多这样的链接:我想同时下载它们,有什么建议吗? 这段代码我用来下载单个文件

 private void txtUrl_TextChanged(object sender, EventArgs e)
    {
        try
        {
            //  function that enter the file name automatically in the savefiledialog.
            Uri uri = new Uri(txtUrl.Text);
            // Save the file name to the string.
            filename = Path.GetFileName(uri.LocalPath);
        }
        catch
        {
            // no need  need an exception message.
        }
    }
    private void DownloadFile(string url, string save)
    {
        using (var client = new WebClient())
        {
            // Run code every time the download changes.
            client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(Changed);
            // Run codes when file download has been completed.
            client.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
                           client.DownloadFileAsync(new Uri(url), save);

解决方案可能如下所示:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;

namespace SillyCSharpNameSpace
{
    public class VerboseDownloaderClassName
    {
        private string downloadFile(string url, string localDir)
        {
            try
            {
                var localPath = Path.Combine(localDir, Path.GetFileName(url));
                using (var wc = new WebClient())
                {
                    wc.DownloadFile(url, localPath);
                    return localPath;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return null;
            }
        }

        public void DownloadAll(List<string> urls)
        {
            urls.AsParallel()
                .Where(url => !string.IsNullOrWhiteSpace(url))
                .WithDegreeOfParallelism(20)
                .Select(url => downloadFile(url, "."));
        }
    }
}

解决方案可能如下所示:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;

namespace SillyCSharpNameSpace
{
    public class VerboseDownloaderClassName
    {
        private string downloadFile(string url, string localDir)
        {
            try
            {
                var localPath = Path.Combine(localDir, Path.GetFileName(url));
                using (var wc = new WebClient())
                {
                    wc.DownloadFile(url, localPath);
                    return localPath;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return null;
            }
        }

        public void DownloadAll(List<string> urls)
        {
            urls.AsParallel()
                .Where(url => !string.IsNullOrWhiteSpace(url))
                .WithDegreeOfParallelism(20)
                .Select(url => downloadFile(url, "."));
        }
    }
}

第一个建议:发布您迄今为止尝试过的代码。第二个建议:解释你到底被困在哪里。我有办法只下载一个文件,但并不实用@DigiFriendLoops are you friend。异步方法也是如此。它们不是@DigiFriend?的其他方法,例如?您的想法是什么?第一个建议:发布您迄今为止尝试过的代码。第二个建议:解释你到底被困在哪里。我有办法只下载一个文件,但并不实用@DigiFriendLoops are you friend。异步方法也是如此。它们不是@DigiFriend?的其他方法,例如?你的想法是什么?不客气。在stackoverflow,他们感谢接受答案的帮助;o) 当然,只是我意识到了不受欢迎。在stackoverflow,他们感谢接受答案的帮助;o) 当然,我不在