C# 我如何计算并显示下载每个文件还需要多少时间?

C# 我如何计算并显示下载每个文件还需要多少时间?,c#,C#,现在,为了使用richTextBox1,我必须在其中显示下载剩余的时间以及下载需要多长时间 有没有更快的方法下载文件,或者httprequest已经足够好了?您可以并行下载它们: Parallel.ForEachimageLinks,节点=> { 图像t=下载图像节点; 矮胖的 { TSave@d:\test\+y++.ToStringD6++.jpg; } } 或者,如果您是在ASP.NET应用程序中下载它们,您最好通过ajax调用从客户端下载它们 你的标题似乎与问题不符,我遗漏了什么?约书亚

现在,为了使用richTextBox1,我必须在其中显示下载剩余的时间以及下载需要多长时间


有没有更快的方法下载文件,或者httprequest已经足够好了?

您可以并行下载它们:

Parallel.ForEachimageLinks,节点=> { 图像t=下载图像节点; 矮胖的 { TSave@d:\test\+y++.ToStringD6++.jpg; } }


或者,如果您是在ASP.NET应用程序中下载它们,您最好通过ajax调用从客户端下载它们

你的标题似乎与问题不符,我遗漏了什么?约书亚·德雷克,里面的问题是一个子问题。主要问题是标题如何计算还剩多少时间以及需要多少时间。最后的问题是另一个问题。
public void GetAllImages()
        {

            // Bing Image Result for Cat, First Page
            string url = "http://www.bing.com/images/search?q=cat&go=&form=QB&qs=n";


            // For speed of dev, I use a WebClient
            WebClient client = new WebClient();
            string html = client.DownloadString(url);

            // Load the Html into the agility pack
            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            doc.LoadHtml(html);

            // Now, using LINQ to get all Images
            /*List<HtmlNode> imageNodes = null;
            imageNodes = (from HtmlNode node in doc.DocumentNode.SelectNodes("//img")
                          where node.Name == "img"
                          && node.Attributes["class"] != null
                          && node.Attributes["class"].Value.StartsWith("sg_t")
                          select node).ToList();*/

           var imageLinks = doc.DocumentNode.Descendants("img")
    .Where(n => n.Attributes["class"].Value == "sg_t")
    .Select(n => HttpUtility.ParseQueryString(n.Attributes["src"].Value)["amp;url"]).ToList();





            foreach (string node in imageLinks)
            {
                y++;
                //Console.WriteLine(node.Attributes["src"].Value);
                richTextBox1.Text += node + Environment.NewLine;
                Image t = DownloadImage(node);
                t.Save(@"d:\test\" + y.ToString("D6" + ".jpg"));

            }


        }


        public Image DownloadImage(string _URL)
        {
            Image _tmpImage = null;

            try
            {
                // Open a connection
                System.Net.HttpWebRequest _HttpWebRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(_URL);

                _HttpWebRequest.AllowWriteStreamBuffering = true;

                // You can also specify additional header values like the user agent or the referer: (Optional)
                //_HttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)";
                //_HttpWebRequest.Referer = "http://www.google.com/";

                // set timeout for 20 seconds (Optional)
                _HttpWebRequest.Timeout = 60000;

                // Request response:
                System.Net.WebResponse _WebResponse = _HttpWebRequest.GetResponse();

                // Open data stream:
                System.IO.Stream _WebStream = _WebResponse.GetResponseStream();

                // convert webstream to image
                _tmpImage = Image.FromStream(_WebStream);

                // Cleanup
                _WebResponse.Close();
                _WebResponse.Close();
            }
            catch (Exception _Exception)
            {
                // Error
                Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
                return null;
            }

            return _tmpImage;
        }