Click here to Skip to main content
15,885,366 members
Articles / Web Development / HTML

A Really Vain "How are my articles doing" Web Spider

Rate me:
Please Sign up or sign in to vote.
4.56/5 (43 votes)
4 Feb 2013CPOL6 min read 91.8K   982   74  
A simple web spider to see fetch CodeProject articles.
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.Win32;


namespace VainWebSpider
{
    #region Program CLASS
    /// <summary>
    /// provides the main access point into the application. Also
    /// provides several generic helper methods, such as InputBox(..),
    /// ErrorBox(..),InfoBox(..) and also provides read/write funtions
    /// to store the current UserID within the registry
    /// </summary>
    public static class Program
    {
        #region Instance fields
        //instance fields
        private static long userId;

        #endregion
        #region Public Methods/Properties

        /// <summary>
        /// gets or sets the UserID which will be used to retrieve codeproject
        /// articles for. When a new UserID is set, the new value is also written
        /// to the windows registry, using the writeToRegistry(..) method. This 
        /// ensures the next time the VainWebSpider application is run, the last
        /// selected UserID will be used. 
        /// </summary>
        public static long UserID
        {
            get { return Program.userId; }
            set
            {
                Program.userId = value;
                Program.writeToRegistry(value);
            }
        }

        /// <summary>
        /// Creates a new "VainWebSpider" subkey (if none exists) under 
        /// the HKEY_LOCAL_MACHINE\SOFTWARE registry key. It also creates
        /// a new value within the newly created VainWebSpider subkey, for
        /// the userId input parameter. This is done so that the VainWebSpider
        /// application can know which user it was looking at last time
        /// </summary>
        /// <param name="userId">The userId to store within the registry</param>
        public static void writeToRegistry(long userId)
        {
            try
            {
                RegistryKey hklm = Registry.CurrentUser;
                RegistryKey hkSoftware = hklm.OpenSubKey("Software", true);
                RegistryKey hkVainWebSpider = hkSoftware.CreateSubKey("VainWebSpider");
                hkVainWebSpider.SetValue("userId", userId);
            }
            catch (Exception)
            {
                Program.ErrorBox("There was a problem creating the Registry key for VainWebSpider");
            }
        }

        /// <summary>
        /// Returns the userId value within the
        /// HKEY_LOCAL_MACHINE\SOFTWARE\VainWebSpider registry key
        /// </summary>
        /// <returns>The value of the userId value within the
        /// HKEY_LOCAL_MACHINE\SOFTWARE\VainWebSpider registry key, 
        /// if it exists, else returns -1</returns>
        public static long readFromRegistry()
        {
            try
            {
                RegistryKey hklm = Registry.CurrentUser;
                RegistryKey hkSoftware = hklm.OpenSubKey("Software");
                RegistryKey hkVainWebSpider = hkSoftware.OpenSubKey("VainWebSpider");
                return long.Parse(hkVainWebSpider.GetValue("userId").ToString());
            }
            catch (Exception)
            {
                return -1;
            }
        }

        /// <summary>
        /// InputBox, returns user input string
        /// </summary>
        /// <param name="prompt">the prompt</param>
        /// <param name="title">the form title</param>
        /// <param name="defaultValue">the default value to use</param>
        /// <returns>the string the user entered</returns>
        public static string InputBox(string prompt,
          string title, string defaultValue)
        {
            InputBoxDialog ib = new InputBoxDialog();
            ib.FormPrompt = prompt;
            ib.FormCaption = title;
            ib.DefaultValue = defaultValue;
            ib.ShowDialog();
            string s = ib.InputResponse;
            ib.Close();
            return s;
        } 

        /// <summary>
        /// Shows an error message within a MessageBox
        /// </summary>
        /// <param name="error">the error message</param>
        public static void ErrorBox(string error)
        {
            MessageBox.Show(error,"Error", MessageBoxButtons.OK,MessageBoxIcon.Error);
        }

        /// <summary>
        /// Shows an information message within a MessageBox
        /// </summary>
        /// <param name="error">the information message</param>
        public static void InfoBox(string info)
        {
            MessageBox.Show(info, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        /// <summary>
        /// Shows a Yes/No query within a MessageBox
        /// </summary>
        /// <param name="query">the query message</param>
        /// <returns>DialogResult, which is the result of the Confirmation query</returns>
        public static DialogResult YesNoBox(string query)
        {
            return MessageBox.Show(query,
                "Confirmation", MessageBoxButtons.YesNo,
                MessageBoxIcon.Question);
        }
        #endregion
        #region MAIN THREAD
        /// <summary>
        /// The main entry point for the application.
        /// Expects 0 command line arguments
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new frmLoader());

        }
        #endregion
    }
    #endregion
}

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
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions