使用C#操作IIS7的辅助类

时间:2021-12-15 17:49:05

一、IIS中相关对象的关系图

使用C#操作IIS7的辅助类

二、IIS对象的浏览与查看

        #region 遍历iis的相关方法
        //相关默认属性的查看参考 http://www.cnblogs.com/devinhua/p/3676546.html

        ///// <summary>
        ///// 通过 服务器 获取 站点相关属性
        ///// </summary>
        //public void ShowSite()
        //{

        //    ServerManager server = new ServerManager();

        //    SiteCollection sites = server.Sites;
        //    foreach (Site site in sites)
        //    {
        //        ApplicationDefaults defaults = site.ApplicationDefaults;

        //        string appPoolName = defaults.ApplicationPoolName;

        //        ConfigurationAttributeCollection attributes = defaults.Attributes;
        //        foreach (ConfigurationAttribute configAttribute in attributes)
        //        {
        //            //......
        //        }

        //        ConfigurationAttributeCollection attributesCollection = site.Attributes;
        //        foreach (ConfigurationAttribute attribute in attributesCollection)
        //        {
        //            //......
        //        }

        //        //
        //        BindingCollection bindings = site.Bindings;
        //        foreach (Microsoft.Web.Administration.Binding binding in bindings)
        //        {
        //            //......
        //        }

        //        //站点状态
        //        ObjectState siteState = site.State;

        //        //站点对应的应用程序池
        //        ApplicationCollection applications = site.Applications;
        //        foreach (Microsoft.Web.Administration.Application application in applications)
        //        {
        //            //......
        //        }
        //    }

        //}

        ///// <summary>
        ///// 通过 站点 获取 应用程序池相关属性
        ///// </summary>
        ///// <param name="site"></param>
        //public void ShowSite(Site site)
        //{
        //    ApplicationCollection applications = site.Applications;
        //    foreach (Microsoft.Web.Administration.Application application in applications)
        //    {

        //        string applicationPoolName = application.ApplicationPoolName;

        //        VirtualDirectoryCollection directories = application.VirtualDirectories;
        //        foreach (VirtualDirectory directory in directories)
        //        {
        //            //......
        //        }
        //    }
        //}

        ///// <summary>
        ///// 通过应用程序池 获取虚拟路径 相关属性
        ///// </summary>
        ///// <param name="application"></param>
        //public void ShowApp(Application application)
        //{

        //    VirtualDirectoryCollection directories = application.VirtualDirectories;
        //    foreach (VirtualDirectory directory in directories)
        //    {
        //        ConfigurationAttributeCollection attributes = directory.Attributes;
        //        foreach (ConfigurationAttribute attribute in attributes)
        //        {
        //            //......
        //        }

        //        ConfigurationChildElementCollection childElements = directory.ChildElements;
        //        foreach (ConfigurationElement element in childElements)
        //        {
        //            //......
        //        }

        //        string path = directory.Path; 
        //        string physicalPath = directory.PhysicalPath;
        //    }

        //}


        ///// <summary>
        ///// 通过服务器 获取 应用程序池相关属性
        ///// </summary>
        //public void ShowApppool()
        //{

        //    ServerManager server = new ServerManager();

        //    ApplicationPoolCollection applicationPools = server.ApplicationPools;
        //    foreach (ApplicationPool pool in applicationPools)
        //    {

        //        bool autoStart = pool.AutoStart;

        //        string runtime = pool.ManagedRuntimeVersion;

        //        string appPoolName = pool.Name;

        //        ProcessModelIdentityType identityType = pool.ProcessModel.IdentityType;

        //        string userName = pool.ProcessModel.UserName;
        //        string password = pool.ProcessModel.Password;
        //    }

        //}

        #endregion



三、使用IIS常用操作辅助类对网站和应用程序池进行增删改查

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Web.Administration;
namespace BaseUtil
{
    public class IisHelper
    {

        /// <summary>
        /// 创建 应用程序池
        /// </summary>
        /// <param name="poolName"></param>
        public static void CreateApppool(string poolName)
        {
            ServerManager server = new ServerManager();

            ApplicationPool myApplicationPool = null;
            if (server.ApplicationPools != null && server.ApplicationPools.Count > 0)
            {
                if (server.ApplicationPools.FirstOrDefault(p => p.Name == poolName) == null)
                {
                    myApplicationPool = server.ApplicationPools.FirstOrDefault(p => p.Name == poolName);
                }
                else
                {
                    myApplicationPool = server.ApplicationPools.Add(poolName);
                }
            }
            else//不存在则创建
            {
                myApplicationPool = server.ApplicationPools.Add(poolName);
            }

            if (myApplicationPool != null)
            {
                myApplicationPool.ProcessModel.IdentityType = ProcessModelIdentityType.NetworkService;

                //可以指定用户
                //myApplicationPool.ProcessModel.IdentityType = ProcessModelIdentityType.SpecificUser;
                //myApplicationPool.ProcessModel.UserName = UserName;
                //myApplicationPool.ProcessModel.Password = Password;

                 
                myApplicationPool.ManagedRuntimeVersion = "v4.0";
                 
                server.CommitChanges();
            }

        }


        /// <summary>
        /// 应用程序池 是否已存在
        /// </summary>
        /// <param name="poolName">应用程序池名称</param>
        /// <returns></returns>
        public static bool IsExistsAppPool(string poolName)
        {

            ServerManager server = new ServerManager();
            if (server.ApplicationPools != null && server.ApplicationPools.Count > 0)
            {
                return server.ApplicationPools.FirstOrDefault(p => p.Name == poolName) != null;
            }
            return false;
        }

        /// <summary>
        /// 添加网站
        /// </summary>
        /// <param name="siteName"></param>
        /// <param name="path">网址文件所在物理文件夹,如:@"c:\MySiteFolder\"</param>
        /// <param name="hostName">域名,任意则为*</param>
        /// <param name="apppoolName">应用程序池名称</param>
        /// <param name="ip">IP</param>
        /// <param name="port">端口</param>
        public static void CreateWebsite(string siteName, string path, string hostName, string apppoolName, string ip = "*", int port = 80)
        {
            ServerManager server = new ServerManager();

            if (server.Sites != null && server.Sites.Count > 0)
            {
                if (server.Sites.FirstOrDefault(s => s.Name ==siteName) == null)//检查站点确实不存在
                {

                    string bindingInfo = string.Format(@"{0}:{1}:{2}", ip, port, hostName);
                    Site site = server.Sites.Add(siteName, "http", bindingInfo, path);
                    site.ApplicationDefaults.ApplicationPoolName = apppoolName;
                    server.CommitChanges();
                }
            }
        }

        /// <summary>
        /// 站点 是否已存在
        /// </summary>
        /// <param name="siteName"></param>
        /// <returns></returns>
        public static bool IsExistsSite(string siteName)
        {
            ServerManager server = new ServerManager();
            if (server.Sites != null && server.Sites.Count > 0)
            {
                return server.Sites.FirstOrDefault(s => s.Name == siteName) != null;
            }

            return false;
        }


    }
}