• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#IIS应用程序池辅助类分类:C#Helper2014-07-1909:50249人阅读评论(0)收藏 ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
using System.Collections.Generic;
using System.DirectoryServices;
using System.Linq;
using Microsoft.Web.Administration;//位于:C:\Windows\System32\inetsrv\Microsoft.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;
        }
    }
}

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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#***类、序列化、函数……发布时间:2022-07-10
下一篇:
C#托管资源与非托管资源发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap