Click here to Skip to main content
15,893,722 members
Articles / Programming Languages / C#

The Code Project Browser Add-in for Visual Studio 2005 and 2008

Rate me:
Please Sign up or sign in to vote.
4.90/5 (108 votes)
27 Mar 2008CPOL9 min read 317.4K   4.9K   296  
An add-in for browsing, downloading and managing CodeProject samples directly in Visual Studio
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Windows.Forms;

namespace CPBrowser
{
    /// <summary>
    /// This class stores the required information necessary to download a Code 
    /// Project zip file.
    /// </summary>
    public class DownloadInfo
    {
        private Uri m_projectUrl = null;
        private Uri m_articleUrl = null;
        private HtmlDocument m_htmlDocument = null;
        private CookieCollection m_cookies = null;
        private string m_unzipDirectory = null;

        public DownloadInfo(Uri projectUrl, Uri articleUrl, HtmlDocument htmlDocument)
        {
            m_projectUrl = projectUrl;
            m_articleUrl = articleUrl;
            m_htmlDocument = htmlDocument;
            ParseCookies(htmlDocument.Cookie);
        }

        public Uri ProjectUrl
        {
            get { return m_projectUrl; }
            set { m_projectUrl = value; }
        }

        public Uri ArticleUrl
        {
            get { return m_articleUrl; }
            set { m_articleUrl = value; }
        }

        public HtmlDocument Document
        {
            get { return m_htmlDocument; }
            set { m_htmlDocument = value; }
        }

        public CookieCollection Cookies
        {
            get { return m_cookies; }
            set { m_cookies = value; }
        }

        public string UnzipDirectory
        {
            get { return m_unzipDirectory; }
            set { m_unzipDirectory = value; }
        }

        /// <summary>
        /// These are the cookies that allow us to remain in a logged in state.
        /// This is required because Code Project requires you to be signed in
        /// to download files.  I got help in learning how to use these from 
        /// http://blogs.msdn.com/dgorti/archive/2005/08/16/452347.aspx
        /// </summary>
        private void ParseCookies(string cookieString)
        {
            try
            {
                string[] token = new String[] { "; " };
                String[] cookieArray = cookieString.Split(token, StringSplitOptions.RemoveEmptyEntries);
                m_cookies = new CookieCollection();
                for (Int32 i = 0; i < cookieArray.Length; i++)
                {
                    char[] tokenChar = new Char[] { '=' };
                    String[] cookie = cookieArray[i].Split(tokenChar, 2, StringSplitOptions.RemoveEmptyEntries);
                    if (cookie.Length == 2)
                        m_cookies.Add(new Cookie(cookie[0], cookie[1]));
                    else if (cookie.Length == 1)
                        m_cookies.Add(new Cookie(cookie[0], String.Empty));
                }
            }
            catch (Exception e)
            {
                string errMsg = "Unable to parse page cookies: " + e.Message;
                throw new Exception(errMsg, e);
            }
        }


    }
}

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
United States United States
SlickEdit Inc. provides software developers with multi-language development tools and the most advanced code editors available. Power programmers, from Fortune 500 companies to individuals, have chosen SlickEdit as their development tool of choice for over 19 years. Proven on Windows, Linux, UNIX, and Mac OS X platforms, SlickEdit products enable even the most accomplished developers to write more code faster, and more accurately. For more information about SlickEdit and free trial downloads, please visit http://www.slickedit.com.
This is a Organisation

1 members

Comments and Discussions