Click here to Skip to main content
15,896,207 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.3K   1.8K   55  
Base library for programmatically managing IIS through C# and Directory Services.
using System;
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices;
using Sarafian.Framework.General.DirectoryManager.Common;
using System.Diagnostics;
using System.IO;

namespace Sarafian.Framework.General.DirectoryManager.IIS
{
    public class Service : Base.BaseEntry
    {
        public const string SCHEMA_CLASS_NAME = "IIsWebService";

        static Service()
        {
            //InitializeVersions();
        }

        public Service(string serviceAddress)
            : base(String.Concat("IIS://", serviceAddress, "/W3SVC"))
        {
            ServiceAddress = serviceAddress;
            Schema = new Schema(ServiceAddress);

        }

        protected override void HandleChild(DirectoryEntry child)
        {
            if (AppPools.IsAppPools(child))
            {
                appPools = new AppPools(child);
                return;
            }
            if (WebSite.IsWebSite(child))
            {
                WebSites.Add(new WebSite(child,this));
                return;
            }
            if (Info.IsInfo(child))
            {
                Info = new Info(child,this);
                return;
            }

        }
        protected override void BeforeRefresh()
        {
            this.appPools = null;
            WebSites = new List<WebSite>();
        }
        protected override string SchemaClassName
        {
            get { return SCHEMA_CLASS_NAME; }
        }


        public DirectoryEntry ServiceEntry { get { return base.SourceEntry; } }

        private AppPools appPools;
        public List<AppPool> ApplicationPools { get { return this.appPools.ApplicationPools; } }
        public List<WebSite> WebSites { get; private set; }
        public string ServiceAddress { get; private set; }
        public Schema Schema { get; private set; }
        public Info Info { get; private set; }
        public bool IsUnderNT { get { return Schema.AppIsolated.IsUnderNT; } }
        //public WebVirtualDir Root { get; private set; }

        public static bool IsService(DirectoryEntry entry)
        {
            return entry.SchemaClassName == SCHEMA_CLASS_NAME;
        }
        public int MajorVersionNumber
        {
            get { return Info.MajorIIsVersionNumber; }
        }

        public int MinorVersionNumber
        {
            get { return Info.MinorIIsVersionNumber; }
        }
        //public static int MajorIIsVersionNumber { get; private set; }
        //public static int MinorIIsVersionNumber { get; private set; }
        //public static bool IsIISInstalled { get; private set; }

        //private static void InitializeVersions()
        //{
        //    try
        //    {
        //        Microsoft.Win32.RegistryKey inetStpKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\InetStp");
        //        MajorIIsVersionNumber=(int)inetStpKey.GetValue("MajorIIsVersionNumber");
        //        MinorIIsVersionNumber = (int)inetStpKey.GetValue("MinorIIsVersionNumber");
        //        IsIISInstalled = true;
        //    }
        //    catch (Exception ex)
        //    {
        //        IsIISInstalled = false;
        //    }
        //}


        public int GetNextAvailableSiteID()
        {
            int maxSiteID = int.MinValue;
            foreach (WebSite site in WebSites)
            {
                if (maxSiteID < site.SiteID)
                {
                    maxSiteID = site.SiteID;
                }
            }
            return maxSiteID + 1;
        }
        public bool PortOccupied(int port)
        {
            foreach (WebSite webSite in WebSites)
            {
                if (webSite.Port == port)
                {
                    return false;
                }
            }
            return true;
        }


        #region Methods
        public WebSite AddWebSite(string siteName, int port, string physicalPath)
        {
            return AddWebSite(siteName, port, physicalPath, null);
        }
        public WebSite AddWebSite(string siteName, int port, string physicalPath, AppPool appPool)
        {
            if (!PortOccupied(port))
            {
                throw new Exception(String.Format("Port {0} is occupied", port.ToString()));
            }
            WebSite webSite=WebSite.Create(this,siteName,port,physicalPath);
            if (appPool != null)
            {
                throw new NotImplementedException();
            }
            base.Refresh();
            return webSite;
        }

        public void Restart()
        {
            string systemPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.System);
            string path = Path.Combine(systemPath, "iisreset.exe");
            Process p = Process.Start(path);
            p.WaitForExit();                

        }

        #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