• C# IIS应用程序池辅助类 分类: C# Helper 2014-07-19 09:50 248人阅读 评论(0) 收藏


    using System.Collections.Generic;
    using System.DirectoryServices;
    using System.Linq;
    using Microsoft.Web.Administration;//位于:C:WindowsSystem32inetsrvMicrosoft.Web.Administration.dll
    
    namespace Whir.Software.InteralServerManage.IISManager
    {
        /// <summary>
        ///     IIS应用程序池辅助类
        /// </summary>
        public class AppPoolService
        {
            protected static string Host = "localhost";
    
            /// <summary>
            ///     取得所有应用程序池
            /// </summary>
            /// <returns></returns>
            public static List<string> GetAppPools()
            {
                var appPools = new DirectoryEntry(string.Format("IIS://{0}/W3SVC/AppPools", Host));
                return (from DirectoryEntry entry in appPools.Children select entry.Name).ToList();
            }
    
            /// <summary>
            ///     取得单个应用程序池
            /// </summary>
            /// <returns></returns>
            public static ApplicationPool GetAppPool(string appPoolName)
            {
                ApplicationPool app = null;
                var appPools = new DirectoryEntry(string.Format("IIS://{0}/W3SVC/AppPools", Host));
                foreach (DirectoryEntry entry in appPools.Children)
                {
                    if (entry.Name == appPoolName)
                    {
                        var manager = new ServerManager();
                        app = manager.ApplicationPools[appPoolName];
                    }
                }
                return app;
            }
    
            /// <summary>
            ///     判断程序池是否存在
            /// </summary>
            /// <param name="appPoolName">程序池名称</param>
            /// <returns>true存在 false不存在</returns>
            public static bool IsAppPoolExsit(string appPoolName)
            {
                bool result = false;
                var appPools = new DirectoryEntry(string.Format("IIS://{0}/W3SVC/AppPools", Host));
                foreach (DirectoryEntry entry in appPools.Children)
                {
                    if (entry.Name.Equals(appPoolName))
                    {
                        result = true;
                        break;
                    }
                }
                return result;
            }
    
            /// <summary>
            ///     删除指定程序池
            /// </summary>
            /// <param name="appPoolName">程序池名称</param>
            /// <returns>true删除成功 false删除失败</returns>
            public static bool DeleteAppPool(string appPoolName)
            {
                bool result = false;
                var appPools = new DirectoryEntry(string.Format("IIS://{0}/W3SVC/AppPools", Host));
                foreach (DirectoryEntry entry in appPools.Children)
                {
                    if (entry.Name.Equals(appPoolName))
                    {
                        try
                        {
                            entry.DeleteTree();
                            result = true;
                            break;
                        }
                        catch
                        {
                            result = false;
                        }
                    }
                }
                return result;
            }
    
            /// <summary>
            ///     创建应用程序池
            /// </summary>
            /// <param name="appPool"></param>
            /// <returns></returns>
            public static bool CreateAppPool(string appPool)
            {
                try
                {
                    if (!IsAppPoolExsit(appPool))
                    {
                        var appPools = new DirectoryEntry(string.Format("IIS://{0}/W3SVC/AppPools", Host));
                        DirectoryEntry entry = appPools.Children.Add(appPool, "IIsApplicationPool");
                        entry.CommitChanges();
                        return true;
                    }
                }
                catch
                {
                    return false;
                }
                return false;
            }
    
            /// <summary>
            ///     编辑应用程序池
            /// </summary>
            /// <param name="application"></param>
            /// <returns></returns>
            public static bool EditAppPool(ApplicationPool application)
            {
                try
                {
                    if (IsAppPoolExsit(application.Name))
                    {
                        var manager = new ServerManager();
                        manager.ApplicationPools[application.Name].ManagedRuntimeVersion = application.ManagedRuntimeVersion;
                        manager.ApplicationPools[application.Name].ManagedPipelineMode = application.ManagedPipelineMode;
                        //托管模式Integrated为集成 Classic为经典
                        manager.CommitChanges();
                        return true;
                    }
                }
                catch
                {
                    return false;
                }
                return false;
            }
        }
    }

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    Windows SDK 之 mciSendString最后一个参数
    java常用包下载地址(非maven)
    windows api(GDI)实现图片旋转
    windows sdk版本 之 并查集生成迷宫
    自签https证书2(适配新版chrome,不会显示“不安全”)
    数据结构——栈(Stacks)
    数据结构——表(list)
    数据结构——链表(linkedlist)
    解题报告1010 诡秘的余数
    函数体中用指针返回数组的方法
  • 原文地址:https://www.cnblogs.com/zhangqs008/p/4687673.html
Copyright © 2020-2023  润新知