【问题标题】:C# Webclient downloading multiple filesC# Webclient 下载多个文件
【发布时间】:2012-04-02 14:38:02
【问题描述】:

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

我的问题是:我要怎么做才能检查第一次下载是否完成然后继续第二次下载等等?

这是我的密码:

        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;
                       });
        }

我已经检查了这个链接:DownloadFileAsync multiple files using webclient,但我真的不明白如何植入这个:(。(我对 C# 很陌生)

【问题讨论】:

    标签: c# webclient


    【解决方案1】:

    DownloadFileCompleted 事件是您知道您已完成文件下载的信号。

    来自 MSDN:

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

    从您的代码中不清楚 webClient 和 webClient2 的声明位置和方式,但本质上,您可以在触发第一个 DownloadFileCompleted 事件时开始第二次下载。但是请注意,如果您使用 WebClient 的 2 个单独实例,您可以同时执行 2 个不同文件的下载。

    【讨论】:

    • Okai thx!所以这意味着我只需在每个 DownloadFileCompleted 事件中放置一个新的网络客户端?编辑:我试图在 DownloadfileCompleted 事件中开始第二次下载,但这似乎不起作用。
    • @Darkshadw 我扩大了一点我的答案。我希望这能说清楚。
    • #Icarus 每当我尝试这样做时:WebClient webClient2 = new WebClient(); MySingleton.Instance.FirstStartProgress = "Downloading Dll... Please stand by..."; webClient2.DownloadFileAsync(new Uri(Dll_HtmlAgilityPackUrl), Dll_HtmlAgilityPackLocation); 它不想开始下载:(。
    【解决方案2】:

    这是您可以根据需要更改的快进代码。

     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");
                }
    
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-09
      • 2015-12-30
      • 1970-01-01
      相关资源
      最近更新 更多