Click here to Skip to main content
15,897,371 members
Articles / Desktop Programming / Windows Forms

Windows Services Made Simple

Rate me:
Please Sign up or sign in to vote.
4.62/5 (10 votes)
27 Jun 2007CPOL10 min read 94.6K   6.9K   69  
Describes how to build a Windows Service using the Pegasus Library.
using System;
using System.Configuration;

namespace Pegasus.Web.Security.SecureWebPage
{

	/// <summary>
	/// Represents a directory entry in the &lt;secureWebPages&gt;
	/// configuration section.
	/// </summary>
	public class SecureWebPageDirectorySetting : SecureWebPageItemSetting {

		#region Constructors

		/// <summary>
		/// Creates an instance of SecureWebPageDirectorySetting.
		/// </summary>
		public SecureWebPageDirectorySetting()
			: base() {
		}
	    
	    /// <summary>
	    /// Creates an instance with an initial path value.
	    /// </summary>
	    /// <param name="path">The relative path to the directory.</param>
		public SecureWebPageDirectorySetting(string path)
	        : base(path) {
	    }

		/// <summary>
	    /// Creates an instance with initial values.
	    /// </summary>
	    /// <param name="path">The relative path to the directory.</param>
	    /// <param name="secure">The type of security for the directory.</param>
		public SecureWebPageDirectorySetting(string path, SecurityType secure)
	        : base(path, secure) {
	    }

		/// <summary>
		/// Creates an instance with initial values.
		/// </summary>
		/// <param name="path">The relative path to the directory or file.</param>
		/// <param name="secure">The type of security for the directory.</param>
		/// <param name="recurse">A flag indicating whether or not to recurse this directory 
		/// when evaluating security.</param>
		public SecureWebPageDirectorySetting(string path, SecurityType secure, bool recurse)
			: this(path, secure) {
			Recurse = recurse;
		}

		#endregion

		#region Properties

		/// <summary>
		/// Gets or sets the path of this directory setting.
		/// </summary>
		[ConfigurationProperty("path", IsRequired = true, IsKey = true), RegexStringValidator(@"^(?:|/|[\w\-][\w\.\-]*(?:/[\w\.\-]+)*/?)$")]
		public override string Path {
			get { return base.Path; }
			set { base.Path = CleanPath(value); }
		}

		/// <summary>
		/// Gets or sets a flag indicating whether or not to include all files in any sub-directories 
		/// when evaluating a request.
		/// </summary>
		[ConfigurationProperty("recurse", DefaultValue = false)]
		public bool Recurse {
			get { return (bool)this["recurse"]; }
			set { this["recurse"] = value; }
		}

		#endregion

		/// <summary>
		/// Overriden to "clean-up" any inconsistent, yet allowed, input.
		/// </summary>
		protected override void PostDeserialize() {
			base.PostDeserialize();
			this["path"] = CleanPath(Path);
		}

		/// <summary>
		/// Cleans the specified path as needed.
		/// </summary>
		/// <param name="path">The path to clean.</param>
		/// <returns>A string containing the cleaned path value.</returns>
		protected string CleanPath(string path) {
			// Strip any trailing slash from the path.
			if (path.EndsWith("/"))
				return Path.Substring(0, Path.Length - 1);
			else
				return path;
		}

	}

	/// <summary>
	/// Represents a collection of SecureWebPageDirectorySetting objects.
	/// </summary>
	public class SecureWebPageDirectorySettingCollection : SecureWebPageItemSettingCollection {

		/// <summary>
		/// Gets the element name for this collection.
		/// </summary>
		protected override string ElementName {
			get { return "directories"; }
		}

		/// <summary>
		/// Gets a flag indicating an exception should be thrown if a duplicate element 
		/// is added to the collection.
		/// </summary>
		protected override bool ThrowOnDuplicate {
			get { return true; }
		}

		/// <summary>
		/// Gets the element at the specified index.
		/// </summary>
		/// <param name="index">The index to retrieve the element from.</param>
		/// <returns>The SecureWebPageDirectorySetting located at the specified index.</returns>
		public SecureWebPageDirectorySetting this[int index] {
			get { return (SecureWebPageDirectorySetting)BaseGet(index); }
		}

		/// <summary>
		/// Gets the element with the specified path.
		/// </summary>
		/// <param name="path">The path of the element to retrieve.</param>
		/// <returns>The SecureWebPageDirectorySetting with the specified path.</returns>
		public new SecureWebPageDirectorySetting this[string path] {
			get { return (SecureWebPageDirectorySetting)BaseGet(path.ToLower()); }
		}

		/// <summary>
		/// Creates a new element for this collection.
		/// </summary>
		/// <returns>A new instance of SecureWebPageFileSetting.</returns>
		protected override ConfigurationElement CreateNewElement() {
			return new SecureWebPageDirectorySetting();
		}

		/// <summary>
		/// Gets the key for the specified element.
		/// </summary>
		/// <param name="element">An element to get a key for.</param>
		/// <returns>A string containing the Path of the SecureWebPageDirectorySetting.</returns>
		protected override object GetElementKey(ConfigurationElement element) {
			if (element != null)
				return ((SecureWebPageDirectorySetting)element).Path.ToLower();
			else
				return null;
		}

	}

}

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
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions