zoukankan      html  css  js  c++  java
  • C#操作IIS类库

    640?wx_fmt=jpeg


    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;

            }


        }



    }


  • 相关阅读:
    DOS常用命令
    SQL查询优化:详解SQL Server非聚集索引(转载)
    SQLServer索引
    SQL注入技术专题—由浅入深【精华聚合贴】
    DVWA 1.9 通关秘籍
    高手养成计划基础篇-Linux第二季
    【摩斯电码】我是如何通过一张小纸条渗透进了妹子的心
    阿里旗下蚂蚁金服副总:有望3-5年内消灭伪基站
    【病毒分析】对一个vbs脚本病毒的分析
    【安全热点】阿里巴巴月饼门,观点两极化,孰对孰错?
  • 原文地址:https://www.cnblogs.com/hgmyz/p/12351407.html
Copyright © 2011-2022 走看看