Click here to Skip to main content
15,889,838 members
Articles / Programming Languages / C#

Generic Directory Watcher Service

Rate me:
Please Sign up or sign in to vote.
4.76/5 (29 votes)
8 Feb 2007GPL314 min read 195.9K   2.8K   148  
This service watches for filesystem events in directories and runs specified programs in response to those events.
using System;
using System.Collections;
using System.Configuration;

namespace DirectoryWatcher
{
    /// <summary>
    /// Collection of referenced assemblies for a snippet of runtime-compiled code.
    /// </summary>
    [ConfigurationCollection(typeof(ReferencedAssembly), AddItemName = "referencedAssembly")]
    public class ReferencedAssemblyCollection : ConfigurationElementCollection
    {
        /// <summary>
        /// Numeric indexer for the collection.
        /// </summary>
        /// <param name="index">
        /// Index in the collection whose contents we are to return.
        /// </param>
        /// <returns>
        /// ReferencedAssembly object at the specified index.
        /// </returns>
        public ReferencedAssembly this[int index]
        {
            get
            {
                return (ReferencedAssembly)base.BaseGet(index);
            }
        }

        /// <summary>
        /// String indexer for the collection; uses the Name property of each ReferencedAssembly
        /// object as the index.
        /// </summary>
        /// <param name="key">
        /// Value of the Name property of the ReferencedAssembly object that we are to return.
        /// </param>
        /// <returns>
        /// ReferencedAssembly object with the specified name.
        /// </returns>
        public new ReferencedAssembly this[string key]
        {
            get
            {
                return (ReferencedAssembly)base.BaseGet(key);
            }
        }

        /// <summary>
        /// Tests to see whether a given element name is a member of this collection.
        /// </summary>
        /// <param name="elementName">
        /// Element name that we are to test.
        /// </param>
        /// <returns>
        /// True of the element name is part of this collection, false otherwise.
        /// </returns>
        protected override bool IsElementName(string elementName)
        {
            if ((string.IsNullOrEmpty(elementName)) || (elementName != "referencedAssembly"))
                return false;

            return true;
        }

        /// <summary>
        /// Collection type of this collection.
        /// </summary>
        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.AddRemoveClearMap;
            }
        }

        /// <summary>
        /// Gets value to use as the key for a specific element in this collection.
        /// </summary>
        /// <param name="element">
        /// Element whose key we are to retrieve.
        /// </param>
        /// <returns>
        /// The Name property of the specified ReferencedAssembly object.
        /// </returns>
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ReferencedAssembly)element).Name;
        }

        /// <summary>
        /// Creates a new element to add to the collection.
        /// </summary>
        /// <returns>
        /// A new ReferencedAssembly object.
        /// </returns>
        protected override ConfigurationElement CreateNewElement()
        {
            return new ReferencedAssembly();
        }
    }
}

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 GNU General Public License (GPLv3)


Written By
Software Developer (Senior) AOC Solutions
United States United States
I'm a software architect in the truest sense of the word: I love writing code, designing systems, and solving tough problems in elegant ways. I got into this industry not for the money, but because I honestly can't imagine myself doing anything else. Over my career, I've worked on just about every major Microsoft web technology, running the gamut from SQL Server 7.0 to 2008, from .NET 1.1 to 4.0, and many others. I've made a name for myself and have risen to my current position by being able to visualize and code complex systems, all while keeping the code performant and extensible.

Both inside and outside of work, I love researching new technologies and using them sensibly to solve problems, not simply trying to force the newest square peg into the round hole. From emerging Microsoft projects like AppFabric to third party or open source efforts like MongoDB, nServiceBus, or ZeroMQ, I'm constantly trying to find the next technology that can eliminate single points of failure or help scale my data tier.

Outside of work, I'm a rabid DC sports fan and I love the outdoors, especially when I get a chance to hike or kayak.

Comments and Discussions