【问题标题】:Download multiple files in C# (one at a time only / queue)在 C# 中下载多个文件(一次仅一个/队列)
【发布时间】:2014-09-16 12:17:31
【问题描述】:

我正在用 C# 编写一个更新程序,我现在需要一些帮助(我是这种语言的新手,请解释你的提示:D)

我的代码:

System.IO.Directory.CreateDirectory("tmp");
            int mmx = updnec.Count();
            label6.Text = "0/" + mmx;
            MessageBox.Show(label6.Text);
            int mmn = 0;
            foreach (string lz in link)
            {

                    MessageBox.Show(lz);
                    client.DownloadFileAsync(new Uri(lz), "tmp/update00" + mmn + ".exe");
                    client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
                    label6.Text = mmn + "/" + mmx;
                    mmn = +1;
                    //Whithout that while{} - part, it tries to download all links from the list at once. But I don't want the program to do that. I don't want it to show a message all the time, too, but when i leave that while{} - part empty, the program just freezes and doesn't even download. 
while (progressBar1.Maximum != progressBar1.Value)

                        {
                        MessageBox.Show("Test");
                    }

            }



        }
        void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            progressBar1.Maximum = (int)e.TotalBytesToReceive;
            progressBar1.Value = (int)e.BytesReceived;
            string sysload = progressBar1.Value /1000000 + "/" + progressBar1.Maximum /1000000 + " MB geladen";
            label12.Text = sysload;
        }

请阅读我的代码中的评论。

如何解决我的问题!?请帮帮我!

重新设计

编辑:

label11.Text = System.DateTime.Now.ToString();
            backgroundWorker1.RunWorkerAsync(link);



        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            List<string> lz = (List<string>)e.Argument; 


            int mmn = 0;    

            progressBar1.Style = ProgressBarStyle.Blocks;
            System.IO.Directory.CreateDirectory("tmp");
            int mmx = 10;
            label6.Text = "0/" + mmx;
            foreach (string lkz in lz)
                using (var client = new WebClient())
            {
                MessageBox.Show("Hallo" + lkz);
                client.DownloadFile(lkz, "tmp/update00" + mmn + ".exe");
            } 


        }

【问题讨论】:

  • 您可以做的是将文件压缩成一个zip文件并将该zip文件发送给客户端。我认为没有其他方法可以做到这一点。查看以下链接了解更多信息
  • 为什么不包括一个问题而不是一个阅读 cmets 的说明?寻求调试帮助的问题必须在问题本身中包含特定问题。
  • @MoezRebai 这对我来说是不可能的:D
  • @Sayse 向您展示我的问题。
  • @Sayse 在评论部分(“//”部分)是我的问题。 Quote: 尽管{} - 部分,它尝试一次下载列表中的所有链接。但我不希望程序这样做。我也不希望它一直显示消息,但是当我将其留空时,{} - 部分为空,程序就会冻结,甚至无法下载。

标签: c# windows file download


【解决方案1】:

尝试使用 BackgroundWorker 并同步而不是异步下载文件。 你可以在这里找到一个例子: http://www.codeproject.com/Tips/83317/BackgroundWorker-and-ProgressBar-demo

这是一个例子:

void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{ 
   var links = new[] { "link1", "link2" }; 

   var percentProgressStep = (100 / links.Length) + 1

   using (var client = new WebClient()) 
   foreach (string l in links) 
     {
       client.DownloadFile(l, Path.GetTempFileName());
       backgroundWorker1.ReportProgress(percentProgressStep);
     } 
}

【讨论】:

  • 我会试试的。谢谢你的回答:)
  • 同步下载文件是什么意思? :O
  • 使用 DownloadFile 而不是 DownloadFileAsync,BackgroundWorker 已经为您异步工作(不会阻止您的表单)。
  • 你能用一段代码告诉我你的意思吗? :)
  • void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { var links = new[] { "link1", "link2" };使用 (var client = new WebClient()) foreach (链接中的字符串 l) { client.DownloadFile(l, Path.GetTempFileName()); } }
猜你喜欢
  • 2019-01-30
  • 2011-07-18
  • 2011-01-03
  • 2019-10-08
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多