⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iiswebsite.cs

📁 用ADSI操作IIS文件
💻 CS
字号:

// IIsAdmin.NET
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This softwareis provided "AS IS" with no warranties of any kind.
// The entire risk arising out of the use or performance of the software
// and source code is with you.
//
// THIS NOTICE MAY NOT BE REMOVED FROM THIS FILE.

using System;
using System.DirectoryServices;

namespace IIsAdmin
{
    /// <summary>
    /// Provides basic information regarding a web site entry in IIs
    /// </summary>
    public class IIsWebSite : IDisposable
    {
        private bool _disposed;        
        private DirectoryEntry _entry;
        
        public event IIsWebSiteEventHandler Started;
        public event IIsWebSiteEventHandler Stopped;

        /// <summary>
        /// Initializes a new instance of the IIsWebSite class
        /// </summary>
        /// <param name="name">The name of the site</param>
        /// <param name="path">The path to the site (ie. IIs://../X)</param>
        /// <param name="index">The index of the site in IIs</param>
        /// <param name="state">The state of the site</param>
        private IIsWebSite(DirectoryEntry entry)
        {
            _entry = entry;                    
        }

        #region IDisposable Members

        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    _entry.Dispose();                    
                }
                _disposed = true;
            }
        }

        #endregion


        /// <summary>
        /// Stops the site
        /// </summary>
        public void Start()
        {
            try
            {
                _entry.Invoke("start");            
            }
            catch(Exception ex)
            {
                ExceptionUtilities.DisplayException(ex);
            }
            
            this.OnStarted(this, new IIsWebSiteEventArgs(this));
        }

        /// <summary>
        /// Starts the site
        /// </summary>
        public void Stop()
        {
            try
            {
                _entry.Invoke("stop");            
            }
            catch(Exception ex)
            {
                ExceptionUtilities.DisplayException(ex);
            }
            
            this.OnStopped(this, new IIsWebSiteEventArgs(this));
        }

        public void Delete()
        {
            try
            {
                this.Stop();
                this._entry.Parent.Invoke("Delete", IIsConstants.IIsWebServerName, this._entry.Name);
            }
            catch(Exception ex)
            {
                ExceptionUtilities.DisplayException(ex);
            }
        }

        /// <summary>
        /// Determines if this site is the active (Started) IIS web site
        /// </summary>
        public bool IsActive
        {
            get
            {
                return (this.State == IIsWebSiteStates.Started);
            }
        }

        /// <summary>
        /// Returns the name of the site
        /// </summary>
        public string Name
        {
            get
            {
                return Convert.ToString(_entry.Invoke("Get", "ServerComment"));                
            }
        }

        /// <summary>
        /// Returns the path to the site (ie. IIs://../X
        /// </summary>
        public string Path
        {
            get
            {
                return _entry.Path;                
            }
        }

        /// <summary>
        /// Returns the index of the site in IIs
        /// </summary>
        public int Index
        {
            get
            {
                return Convert.ToInt32(_entry.Name);                
            }
        }

        /// <summary>
        /// Returns the state of the site (ie. Started, Stopped, Paused etc.)
        /// </summary>
        public IIsWebSiteStates State
        {
            get
            {
                return (IIsWebSiteStates)Convert.ToInt32(_entry.Invoke("Get", "ServerState"));;                
            }
        }

        /// <summary>
        /// Raises the Started event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected virtual void OnStarted(object sender, IIsWebSiteEventArgs e)
        {
            if (this.Started != null)
            {
                this.Started(sender, e);
            }
        }

        /// <summary>
        /// Raises the Stopped event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected virtual void OnStopped(object sender, IIsWebSiteEventArgs e)
        {
            if (this.Stopped != null)
            {
                this.Stopped(sender, e);
            }
        }

        /// <summary>
        /// Returns information about the site as a string
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return string.Format("IIsWebServer: Index = {0}, Name = {1}, State = {2}, Path = {3}", this.Index, this.Name, this.State, this.Path);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entry"></param>
        /// <returns></returns>
        public static IIsWebSite FromDirectoryEntry(DirectoryEntry entry)
        {
            if (entry == null)
            {
                throw new ArgumentNullException("entry", "A valid DirectoryEntry object instance is required to create an IIsWebSite object instance.");            
            }

            // create a new site
            return new IIsWebSite(entry);                                                
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -