【问题标题】:How can i download a file faster?我怎样才能更快地下载文件?
【发布时间】:2015-04-29 07:14:36
【问题描述】:

我用 C# 制作了一个下载程序。它是一个队列下载器。 你可以在这里看到它是如何工作的:点击Express Downloader

有没有更快的下载方法? 这是我使用的方法,它必须支持恢复支持。

private void Download(object startPoint)
    {
        try
        {
            try
            {
                //int startPointInt = Convert.ToInt32(startPoint);
                Int64 startPointInt = Convert.ToInt64(startPoint);
                webRequest = (HttpWebRequest)WebRequest.Create(url);
                webRequest.AddRange(startPointInt);
                webRequest.Credentials = CredentialCache.DefaultCredentials;
                webResponse = (HttpWebResponse)webRequest.GetResponse();
                Int64 fileSize = webResponse.ContentLength;
                strResponse = webResponse.GetResponseStream();
                if (startPointInt == 0)
                {
                    strLocal = new FileStream(txtPath.Text + "\\" + filename, FileMode.Create, FileAccess.Write, FileShare.None);
                }
                else
                {
                    strLocal = new FileStream(txtPath.Text + "\\" + filename, FileMode.Append, FileAccess.Write, FileShare.None);
                }
                int bytesSize = 0;
                byte[] downBuffer = new byte[4096];
                while ((bytesSize = strResponse.Read(downBuffer, 0, downBuffer.Length)) > 0)
                {
                    strLocal.Write(downBuffer, 0, bytesSize);
                    this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { strLocal.Length, fileSize + startPointInt });

                    if (goPause == true)
                    {
                        break;
                    }
                }
            }
            catch { }
        }
        finally
        {

            strResponse.Close();
            strLocal.Close();
        }
    }

【问题讨论】:

  • 获得更快的宽带?
  • @MattWilko 丁丁丁。我们有一个赢家!
  • 你可以查看http压缩:compositecode.com/2009/03/24/…
  • 使用WebClient,它为您封装了所有这些工作,并拥有大量异步和同步方法
  • WebClient 糟透了,我无法下载超过 2GB 的文件...而且我的“on steam”下载速度大约为 13.4 MB/s...所以我希望我的程序以大约 13.4 MB/s 的速度下载速度也...

标签: c# file download queue


【解决方案1】:

你必须做两件事,

  1. 获得更快的带宽
  2. 将块大小从 4096 更改为 8192 或任何可以轻松保存在内存中的数字。

【讨论】:

  • 我可以创建一个方法来检查块大小可以有多高吗?就像一些分析计算机的代码......并找到可用的最大块大小?
  • 在理想情况下,我猜你的块大小可以达到你的 RAM 大小。但我建议块大小为 64MB
  • 那么是否有任何代码可以分析计算机规格以获得最佳块大小?
  • 所以设置为 64 mb,那么我需要在缓冲区中写入 64000 吗?
  • 其实是64000000
猜你喜欢
  • 2020-04-06
  • 1970-01-01
  • 2019-12-16
  • 1970-01-01
  • 1970-01-01
  • 2011-06-14
  • 2015-11-22
  • 1970-01-01
  • 2014-07-19
相关资源
最近更新 更多