Click here to Skip to main content
15,885,278 members
Articles / Multimedia / GDI+

MyDownloader: A Multi-thread C# Segmented Download Manager

Rate me:
Please Sign up or sign in to vote.
4.95/5 (418 votes)
12 Feb 2008CPOL10 min read 2.1M   124.7K   1.3K  
Sample application that manages multiple segmented downloads and supports HTTP, FTP and YouTube video downloads
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using MyDownloader.Spider.Web.Rules;
using MyDownloader.Core;
using MyDownloader.Extension.AutoDownloads;
using MyDownloader.Core.UI;
using MyDownloader.Spider.Web;

namespace MyDownloader.Spider.UI
{
    public partial class WebSpider : UserControl
    {
        public WebSpider()
        {
            InitializeComponent();

            EnterDownloadMode(false);

            UpdateInputBasesOnCheckBoxes();
        }

        Spider siteSpider;

        private void btnDownloadSite_Click(object sender, EventArgs e)
        {
            ResourceLocation rl = this.location1.ResourceLocation;

            rl.BindProtocolProviderType();

            if (rl.ProtocolProviderType == null)
            {
                MessageBox.Show("Invalid URL format, please check the location field.",
                    AppManager.Instance.Application.MainForm.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            AutoDownloadsExtension scheduler;

            scheduler = (AutoDownloadsExtension)AppManager.Instance.Application.GetExtensionByType(typeof(AutoDownloadsExtension));

            if (! scheduler.Active)
            {
                using (StartAutoDownloadsForm startAd = new StartAutoDownloadsForm())
                {
                    if (startAd.ShowDialog() == DialogResult.Yes)
                    {
                        startAd.ScheduledDownloadEnabler.ApplySettings();
                    }
                }

                if (!scheduler.Active)
                {
                    return;
                }
            }

            SpiderContext spiderCntx = new SpiderContext(
                folderBrowser1.Folder,
                rl.URL);

            spiderCntx.NamingRules.Add(
                new BaseLocationNamingRule(chkImagesFromExternal.Checked, chkResourcesFromExternal.Checked));

            if (chkExcludeExtensions.Checked)
            {
                spiderCntx.NamingRules.Add(new ExcludeExtensionNamingRule(txtExcludeExt.Text.Split(',')));
            }
            if (chkExcludeNames.Checked)
            {
                spiderCntx.NamingRules.Add(new ExcludeNameNamingRule(txtExcludeNames.Text.Split(',')));
            }
            if (numDownloadDepth.Value > 0)
            {
                spiderCntx.NamingRules.Add(new MaxDepthNamingRule((int)numDownloadDepth.Value));
            }
            if (chkRemoveCompleted.Checked)
            {
                spiderCntx.ResourceCompleted += delegate(SpiderContext cntx, ISpiderResource resource)
                {
                    DownloadManager.Instance.ClearEnded();
                };
            }

            siteSpider = new Spider(spiderCntx, new WebSpiderResourceFactory());
            siteSpider.Completed += new CompletedEventHandler(siteSpider_Completed);
            siteSpider.Aborted += new EventHandler(siteSpider_Aborted);
            siteSpider.BeginLookup();

            EnterDownloadMode(true);
        }

        private void EnterDownloadMode(bool downloading)
        {
            RefreshStatus();

            progDownload.Value = 0;
            tabParams.Enabled = !downloading;
            btnAbort.Enabled = downloading;
            btnDownloadSite.Enabled = !downloading;
            waitControl1.Visible = downloading;
            tmrRefresh.Enabled = downloading;
        }

        void siteSpider_Aborted(object sender, EventArgs e)
        {
            this.BeginInvoke((MethodInvoker)delegate() { EnterDownloadMode(false); });
        }

        void siteSpider_Completed(SpiderContext cntx)
        {
            this.BeginInvoke((MethodInvoker)delegate() { EnterDownloadMode(false); });
        }

        private void btnAbort_Click(object sender, EventArgs e)
        {
            siteSpider.Abort();            
        }

        private void tmrRefresh_Tick(object sender, EventArgs e)
        {
            RefreshStatus();
        }

        private void RefreshStatus()
        {
            float totalVisited; 
            float totalDownloaded;

            if (siteSpider != null)
            {
                totalVisited = siteSpider.Context.VisitedLocations.Count;
                totalDownloaded = siteSpider.Context.CompletedResources.Count;
            }
            else
            {
                totalVisited = totalDownloaded = 0;
            }

            lblTotalFiles.Text = totalVisited.ToString();
            lblDownloadedFiles.Text = totalDownloaded.ToString();

            try
            {
                if (totalVisited != 0)
                {
                    progDownload.Value = (int)((totalDownloaded / totalVisited) * 100);
                }
                else
                {
                    progDownload.Value = 0;
                }
            }
            catch (Exception)
            {
            }
        }

        private void check_CheckedChanged(object sender, EventArgs e)
        {
            UpdateInputBasesOnCheckBoxes();
        }

        private void UpdateInputBasesOnCheckBoxes()
        {
            txtExcludeExt.Enabled = chkExcludeExtensions.Checked;
            txtExcludeNames.Enabled = chkExcludeNames.Checked;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Brazil Brazil
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions