Click here to Skip to main content
15,892,927 members
Articles / Web Development / IIS

Programmatically Manage IIS

Rate me:
Please Sign up or sign in to vote.
4.48/5 (12 votes)
27 Nov 2008CPOL5 min read 74.1K   1.8K   55  
Base library for programmatically managing IIS through C# and Directory Services.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
using Sarafian.Framework.General.DirectoryManager.Common;

namespace Sarafian.Framework.General.DirectoryManager.Base
{
    public abstract class BaseEntry : IDisposable
    {

        protected BaseEntry(DirectoryEntry entry)
        {
            SourceEntry = entry;
            GuardValidEntry();
            Refresh();
        }

        protected BaseEntry(string connectionString, string userName, string password)
            : this(new DirectoryEntry(connectionString, userName, password))
        {

        }
        protected BaseEntry(string connectionString)
            : this(connectionString, null, null)
        {

        }

        public DirectoryEntry SourceEntry { get; private set; }
        public DirectoryEntries Children { get { return SourceEntry.Children; } }
        public PropertyValues Properties { get; private set; }

        public void Refresh()
        {
            BeforeRefresh();
            string[] propertyNames = GetSpecificPropertyNames();
            if (propertyNames == null)
            {
                Properties = new PropertyValues(SourceEntry);
            }
            else
            {
                Properties = new PropertyValues(SourceEntry, propertyNames);
            }
            foreach (DirectoryEntry child in Children)
            {
                HandleChild(child);
            }
        }
        protected virtual void GuardValidEntry()
        {
            if (SourceEntry.SchemaClassName == SchemaClassName)
            {
                return;
            }
            throw new Exception("Invalide SchemaClassName");
        }
        protected virtual string[] GetSpecificPropertyNames()
        {
            return null;
        }

        protected abstract void HandleChild(DirectoryEntry child);
        protected abstract void BeforeRefresh();
        protected abstract string SchemaClassName { get; }
        public bool HasError { get { return Properties.HasError; } }
        public string Name
        {
            get { return SourceEntry.Name; }
        }

        public void CommitChanges()
        {
            SourceEntry.CommitChanges();
            Refresh();
        }
        
        #region IDisposable Members
        private void GuardDisposed()
        {
            if (this.disposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }
        }
        private bool disposed = false;
        public void Dispose()
        {
            if (!this.disposed)
            {
                return;
            }
            this.disposed = true;
            //TODO: Implement Disposing
            throw new NotImplementedException();

            System.GC.SuppressFinalize(this);
        }

        #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
Team Leader ALGOSYSTEMS
Greece Greece
I live in Athens Greece and currently I am working with Business scale application with .NET latest technologies

I've been developing applications for personal and friends usage with C++ using majorly Borland's various IDEs since 1994.
In 2002 I began working for an R&D institute where I was introduced to C# which I worships ever since.

I love core application development and I would like to publish more articles here and on my blog, but there is not enough time to do so.

I usualy "waste" my spare time watching sitcoms, preferable SCI-FI.
I would like to play chess but I can't find any real world players to hang out with.

Comments and Discussions