(目前最新)《万门JavaScript零基础进阶班 [》

using Microsoft.Web.Administration;

using System;

using System.Collections.Generic;

using System.DirectoryServices;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

//https://docs.microsoft.com/en-us/iis/configuration/system.webServer/security/ipSecurity/add ip地址限定

namespace Network.Housekeeper.Core.IISManger

{

    public class IISUtitls

    {

        /// <summary>

        /// 判断应用程序池是否存在

        /// </summary>

        /// <param name="AppPoolName"></param>

        /// <returns></returns>

        private static bool IsAppPoolName(string AppPoolName)

        {

            bool result = false;

            DirectoryEntry appPools = new DirectoryEntry("IIS://localhost/W3SVC");

            foreach (DirectoryEntry getdir in appPools.Children)

            {

                if (getdir.Name.Equals(AppPoolName))

                {

                    result = true;

                }

            }

            return result;

        }

        /// <summary>

        /// 返回所有的站点Site 类

        /// </summary>

        /// <returns></returns>

        public static List<Site> GetAllSite()

        {

            List<Site> siteStr = new List<Site>();

            ServerManager sm = new ServerManager();

            foreach (var item in sm.Sites)

            {

                siteStr.Add(item);

            }

            return siteStr;

        }

        /// <summary>

        /// 开启站点

        /// </summary>

        /// <param name="siteName"></param>

        /// <returns></returns>

        public static bool SetSiteStart(string siteName)

        {

            ServerManager sm = new ServerManager();

            sm.Sites[siteName].Start();

            return true;

        }

        /// <summary>

        /// 暂定站点

        /// </summary>

        /// <param name="siteName"></param>

        /// <returns></returns>

        public static bool SetSiteStop(string siteName)

        {

            ServerManager sm = new ServerManager();

            sm.Sites[siteName].Stop();

            return true;

        }

        /// <summary>

        /// 删除指定程序池

        /// </summary>

        /// <param name="AppPoolName">程序池名称</param>

        /// <returns>true删除成功 false删除失败</returns>

        private static bool DeleteAppPool(string AppPoolName)

        {

            bool result = false;

            DirectoryEntry appPools = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");

            foreach (DirectoryEntry getdir in appPools.Children)

            {

                if (getdir.Name.Equals(AppPoolName))

                {

                    try

                    {

                        getdir.DeleteTree();

                        result = true;

                    }

                    catch

                    {

                        result = false;

                    }

                }

            }

            return result;

        }

        private static DirectoryEntry CreateAppPool(string AppPoolName)

        {

            DirectoryEntry newpool = new DirectoryEntry();

            if (!IsAppPoolName(AppPoolName))

            {

                DirectoryEntry appPools = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");

                newpool = appPools.Children.Add(AppPoolName, "IIsApplicationPool");

                newpool.CommitChanges();

            }

            #region 修改应用程序的配置(包含托管模式及其NET运行版本)

            #endregion

            return newpool;

        }

        public static void CreateWebSite(string AppPoolName, string BindString, string webPath)

        {

            DirectoryEntry newpool = new DirectoryEntry();

            ServerManager sm = new ServerManager();

            DirectoryEntry appPools = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");

            DirectoryEntry Children = new DirectoryEntry();

            DirectoryEntry root;

            bool webserver = false;

            foreach (DirectoryEntry server in appPools.Children)

            {

                if (server.Name.Equals(AppPoolName))

                    webserver = true;

            }

            if (!webserver)

            {

                CreateAppPool(AppPoolName);

            }

            appPools.CommitChanges();

            DirectoryEntry appRootPools = new DirectoryEntry("IIS://localhost/W3SVC");

            int index = 0;

            foreach (DirectoryEntry server in appRootPools.Children)

            {

                if (server.SchemaClassName == "IIsWebServer")

                {

                    if (server.Properties["ServerComment"][0].ToString() == AppPoolName)

                    {

                        throw new Exception("website:" + AppPoolName + " already exsit.");

                    }

                    if (Convert.ToInt32(server.Name) > index)

                    {

                        index = Convert.ToInt32(server.Name);

                    }

                }

            }

            index++; // new index created         

            Children = appRootPools.Children.Add(index.ToString(), "IIsWebServer");

            Children.CommitChanges();

            Children.Properties["ServerComment"].Clear();

            Children.Properties["ServerComment"].Add(AppPoolName);

            Children.Properties["Serverbindings"].Clear();

            foreach (var item in BindString.Split(','))

            {

                if (!string.IsNullOrWhiteSpace(item))

                    Children.Properties["Serverbindings"].Add($":80:{item}");

            }

            Children.CommitChanges();

            root = Children.Children.Add("ROOT", "IIsWebVirtualDir");

            root.CommitChanges();

            if (string.IsNullOrEmpty(AppPoolName))

            {

                root.Invoke("AppCreate", true);//创建应用程序

            }

            else

            {

                root.Invoke("appCreate3", 0, AppPoolName, true);//

            }

            root.Properties["path"].Clear();

            root.Properties["path"].Add(webPath);

            root.Properties["AppPoolId"].Value = AppPoolName;

            root.Properties["DefaultDoc"][0] = "index.html";//设置默认文档

            root.Properties["AppFriendlyName"].Clear();

            root.Properties["AppIsolated"].Clear();

            root.Properties["AccessFlags"].Clear();

            root.Properties["FrontPageWeb"].Clear();

            root.Properties["AppFriendlyName"].Add(AppPoolName);

            root.Properties["AppIsolated"].Add(2);

            root.Properties["AccessFlags"].Add(513);

            root.Properties["FrontPageWeb"].Add(1);

            root.CommitChanges();

            sm.Sites[AppPoolName].Start();

            sm.ApplicationPools[AppPoolName].ManagedRuntimeVersion = "v4.0";

            sm.ApplicationPools[AppPoolName].ManagedPipelineMode = ManagedPipelineMode.Integrated; //托管模式Integrated为集成 Classic为经典

            sm.CommitChanges();

        }

        public static void UpdateIISSite(string AppPoolName, string BindString)

        {

            DirectoryEntry appRootPools = new DirectoryEntry("IIS://localhost/W3SVC");

            foreach (DirectoryEntry server in appRootPools.Children)

            {

                if (server.SchemaClassName == "IIsWebServer")

                {

                    if (server.Properties["ServerComment"][0].ToString() == AppPoolName)

                    {

                        server.Properties["Serverbindings"].Clear();

                        foreach (var item in BindString.Split(','))

                        {

                            if (!string.IsNullOrWhiteSpace(item))

                                server.Properties["Serverbindings"].Add($":80:{item}");

                        }

                        server.CommitChanges();

                    }

                }

            }

        }

        /// <summary>

        /// 判断站点是否存在

        /// </summary>

        /// <param name="siteName"></param>

        /// <returns></returns>

        public static bool SiteExite(string siteName)

        {

            bool t = false;

            ServerManager sm = new ServerManager();

            Site s = sm.Sites[siteName];

            if (s != null)

                t = true;

            return t;

        }

        /// <summary>

        /// 删除站点

        /// </summary>

        /// <param name="siteName"></param>

        /// <returns></returns>

        public static bool DeleteSite(string siteName)

        {

            bool t = false;

            ServerManager sm = new ServerManager();

            Site s = sm.Sites[siteName];

            if (s != null)

                t = true;

            if (t)

                sm.Sites.Remove(s);

            return t;

        }

    }

}

猜你喜欢

转载自blog.csdn.net/weixin_44864196/article/details/89059748