专业网站建设品牌,十四年专业建站经验,服务6000+客户--广州京杭网络
免费热线:400-683-0016      微信咨询  |  联系我们

AppPoolService-IIS应用程序池辅助类(C#控制应用程序池操作)

当前位置:网站建设 > 技术支持
资料来源:网络整理       时间:2023/2/14 0:45:37       共计:3619 浏览
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.IISManager.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;
        }
    }
}
版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。
·上一条:WinForm 脱离IIS承载Aspx 请求 | ·下一条:C#邮件发送(含附件)

Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有    粤ICP备16019765号 

广州京杭网络科技有限公司 版权所有