Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#网络客户端下载多个文件_C#_Webclient - Fatal编程技术网

C#网络客户端下载多个文件

C#网络客户端下载多个文件,c#,webclient,C#,Webclient,我还有一个问题:(. 我正在尝试为我的应用程序下载多个文件 我的问题是:我必须做什么来检查第一次下载是否完成,然后继续第二次下载,依此类推 这是我的代码: private void DownloadBukkit() { MySingleton.Instance.FirstStartProgress = "Downloading Bukkit.jar... Please stand by..."; webClient.

我还有一个问题:(.
我正在尝试为我的应用程序下载多个文件

我的问题是:我必须做什么来检查第一次下载是否完成,然后继续第二次下载,依此类推

这是我的代码:

        private void DownloadBukkit()
        {
            MySingleton.Instance.FirstStartProgress = "Downloading Bukkit.jar... Please stand by...";
            webClient.DownloadFileAsync(new Uri(MySingleton.Instance.BukkitDownloadLink), Jar_Location);
            webClient.DownloadProgressChanged += backgroundWorker1_ProgressChanged;
            webClient.DownloadFileCompleted += (webClient_DownloadFileCompleted);
        }

        private void DownloadDll()
        {
            if (!webClient.IsBusy)
            {
                MySingleton.Instance.FirstStartProgress = "Downloading HtmlAgilityPack.dll... Please stand by...";
                webClient2.DownloadFileAsync(new Uri(Dll_HtmlAgilityPackUrl), Dll_HtmlAgilityPackLocation);
                webClient2.DownloadProgressChanged += backgroundWorker1_ProgressChanged;
                webClient2.DownloadFileCompleted += (webClient2_DownloadFileCompleted);
            }
        }

    void webClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
    {
            DownloadDll();
    }


    void webClient2_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
    {
         Invoke((MethodInvoker)
               Close);
    }

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            Invoke((MethodInvoker)
                   delegate
                       {
                           labelProgress.Text = MySingleton.Instance.FirstStartProgress;
                           progressBar1.Value = e.ProgressPercentage;
                       });
        }
我已经检查了这个链接:但是我真的不明白如何植入这个:((我对C#很陌生)

这个事件是你知道你已经完成下载文件的信号

从MSDN:

每次异步文件下载操作时都会引发此事件 完成


您的代码不清楚webClient和webClient 2在何处以及如何声明,但基本上,您可以在触发第一个DownloadFileCompleted事件时开始第二次下载。但是,请注意,如果您在此处使用两个独立的
webClient实例,您可以同时执行两个不同文件的下载是快进代码,您可以根据需要更改它

 WebClient client = null;
        public FileDownloader()
        {
            InitializeComponent();
            client = new WebClient();
            client.DownloadProgressChanged += client_DownloadProgressChanged;
            client.DownloadFileCompleted += client_DownloadFileCompleted;
        }

        void client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            lblMessage.Text = "File Download Compeleted.";
        }

        void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            lblMessage.Text = e.ProgressPercentage + " % Downloaded.";
        }

        private void StartDownload(object sender, RoutedEventArgs e)
        {
            if (client == null)
                client = new WebClient();

//Loop thru your files for eg. file1.dll, file2.dll .......etc.
            for (int index = 0; index < 10; index++)
            {
                //loop on files 
                client.DownloadFileAsync(
                    new Uri(
                            @"http://mywebsite.com/Files/File" + index.ToString() + ".dll"
                            , UriKind.RelativeOrAbsolute),
                    @"C:\Temp\file+" + index.ToString() + ".dll");
            }

        }
WebClient客户端=null;
公共文件下载程序()
{
初始化组件();
客户端=新的WebClient();
client.DownloadProgressChanged+=客户端\ u DownloadProgressChanged;
client.DownloadFileCompleted+=client_DownloadFileCompleted;
}
无效客户端\u下载文件已完成(对象发送方,System.ComponentModel.AsyncCompletedEventArgs e)
{
lblMessage.Text=“文件下载完成。”;
}
无效客户端\u DownloadProgressChanged(对象发送方,DownloadProgressChangedEventArgs e)
{
lblMessage.Text=e.ProgressPercentage+“%download.”;
}
私有void StartDownload(对象发送方、路由目标方)
{
if(客户端==null)
客户端=新的WebClient();
//循环浏览文件,例如file1.dll、file2.dll……等。
对于(int-index=0;index<10;index++)
{
//循环文件
client.DownloadFileAsync(
新Uri(
@"http://mywebsite.com/Files/File“+index.ToString()+”.dll”
,UriKind.相对溶质),
@“C:\Temp\file+”+index.ToString()+”.dll”);
}
}

Okai thx!这意味着我只需要在每个DownloadFileCompleted事件中添加一个新的webclient?编辑:我已经尝试在DownloadFileCompleted事件中启动第二次下载,但似乎不起作用。@Darkshadw我扩展了一点我的答案。我希望这能说明问题。35;Icarus每当我尝试这样做时:
webclient webClient2=new-WebClient();MySingleton.Instance.FirstStartProgress=“下载Dll…请准备…”;webClient2.DownloadFileAsync(新Uri(Dll\u HtmlAgilityPackUrl),Dll\u HtmlAgilityPackLocation);
它不想开始下载:(。