Пример #1
0
        public void DownloadFileAsync(Uri uri, string savePath, int segmentsCount)
        {
            st.Start();
            if (segmentsCount < 1)
            {
                throw new ArgumentOutOfRangeException("segmentsCount");
            }

            int?size = GetFileSize(uri);

            if (!size.HasValue)
            {
                segmentsCount = 1;
            }
            else if (size.Value < MIN_FILE_SIZE)
            {
                segmentsCount = 1;
            }

            _savePath = savePath;
            using (File.Create(_savePath)) { }

            _webClients = new List <RangeWebClient>();
            for (int i = 0; i < segmentsCount; i++)
            {
                var client = new RangeWebClient();

                if (segmentsCount > 1)
                {
                    var range = GetRange(i, segmentsCount, size.Value);
                    client.From = range.Item1;
                    client.To   = range.Item2;
                }

                client.DownloadProgressChanged += (s, e) => OnDownloadProgressChanged(e);
                client.DownloadFileCompleted   += client_DownloadFileCompleted;
                client.Filename = Path.ChangeExtension(_savePath, Path.GetExtension(_savePath) + "." + i.ToString("D3"));
                _webClients.Add(client);
            }
            _webClients.ForEach(client => client.DownloadFileAsync(uri));
        }
Пример #2
0
        public void DownloadFileAsync(Uri uri, string savePath, int segmentsCount = 1)
        {
            if (segmentsCount < 1)
                throw new ArgumentOutOfRangeException("segmentsCount");

            int? size = GetFileSize(uri);

            if (!size.HasValue)
                segmentsCount = 1;
            else if (size.Value < MIN_FILE_SIZE)
                segmentsCount = 1;

            _savePath = savePath;
            using (File.Create(_savePath))
            {
                // Create empty file to make sure that no write restrictions
            }

            _webClients = new List<RangeWebClient>();

            for (int i = 0; i < segmentsCount; i++)
            {
                var client = new RangeWebClient();

                if (segmentsCount > 1)
                {
                    var range = GetRange(i, segmentsCount, size.Value);
                    client.From = range.Item1;
                    client.To = range.Item2;
                }

                client.DownloadProgressChanged += (s, e) => OnDownloadProgressChanged(e);
                client.DownloadFileCompleted += client_DownloadFileCompleted;
                client.Filename = Path.ChangeExtension(_savePath, Path.GetExtension(_savePath) + "." + i.ToString("D3"));
                _webClients.Add(client);
            }

            _webClients.ForEach(client => client.DownloadFileAsync(uri));
        }