wpf

[wpf] IIS제어 프로그램 소스

동동(이재동) 2009. 9. 22. 17:17
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;

namespace IISControler
{
    /// <summary>
    /// IIS Controler 
    /// 2009-09-22 jdlee
    /// </summary>

    class IISControl
    {
        string MetabasePath = "IIS://Localhost/W3SVC";

        public IISControl(string MetabasePath)
        {
            this.MetabasePath = MetabasePath;
        }

        internal void DeleteSite(string SiteName)
        {
            try
            {
                DirectoryEntry directoryEntry = new DirectoryEntry(MetabasePath + "/" + GetSiteId(SiteName));
                directoryEntry.DeleteTree();
            }
            catch (Exception)
            {                
                throw;
            }
        }

        internal void StopSite(string SiteName)
        {
            try
            {
                DirectoryEntry directoryEntry = new DirectoryEntry(MetabasePath + "/" + GetSiteId(SiteName));
                directoryEntry.Invoke("Stop", null);
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        internal void StartSite(string SiteName)
        {
            try
            {
                DirectoryEntry directoryEntry = new DirectoryEntry(MetabasePath + "/" + GetSiteId(SiteName));
                directoryEntry.Invoke("Start", null);
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        internal void CreateSite(string SiteId, string SiteName, string SitePath)
        {
            try
            {
                DirectoryEntry directoryEntry = new DirectoryEntry(MetabasePath);
                string className = directoryEntry.SchemaClassName.ToString();

                if (className.EndsWith("Service"))
                {
                    DirectoryEntries sites = directoryEntry.Children;
                    DirectoryEntry newSite = sites.Add(SiteId, (className.Replace("Service", "Server")));
                    newSite.Properties["ServerComment"][0] = SiteName;
                    newSite.CommitChanges();

                    DirectoryEntry newRoot;
                    newRoot = newSite.Children.Add("Root", "IIsWebVirtualDir");
                    newRoot.Properties["Path"][0] = SitePath;
                    newRoot.Properties["AccessScript"][0] = true;
                    newRoot.CommitChanges();
                }
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        internal void SetSiteBinding(string SiteName, string BindingInfo)
        {
            string propertyName = "ServerBindings";

            try
            {
                DirectoryEntry directoryEntry = new DirectoryEntry(MetabasePath + "/" + GetSiteId(SiteName));

                if (directoryEntry.Properties[propertyName].Value == null)
                    directoryEntry.Properties[propertyName].Add(BindingInfo);
                else
                    directoryEntry.Properties[propertyName][0] = BindingInfo;

                directoryEntry.CommitChanges();
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        internal string GetStatusInfo(string SiteName)
        {
            string Status = "Null";
            
            if (GetSiteId(SiteName) != null)
            {
                DirectoryEntry directoryEntry = new DirectoryEntry(MetabasePath + "/" + GetSiteId(SiteName));

                switch (directoryEntry.Properties["ServerState"].Value.ToString())
                {
                    case "2":
                        return Status = "Start";
                    case "4":
                        return Status = "Stop";
                    case "6":
                        return Status = "Pause";
                }
            }        
            
            return Status;
        }

        private string GetSiteId(string webSiteName)
        {
            try
            {
                DirectoryEntry root = new DirectoryEntry(MetabasePath);
                string siteid = null;

                foreach (DirectoryEntry item in root.Children)
                {
                    if (item.SchemaClassName == "IIsWebServer")
                    {
                        if (webSiteName == item.Properties["ServerComment"].Value.ToString())
                            siteid = item.Name;
                    }
                }

                if (siteid == null)
                    return null;

                return siteid;
            }
            catch (Exception)
            {
                
                throw;
            }
        }
    }
}