隐藏

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

发布:2020/4/24 16:28:13作者:管理员 来源:本站 浏览次数:1207

  1. using System.Collections.Generic;
  2. using System.DirectoryServices;
  3. using System.Linq;
  4. using Microsoft.Web.Administration; //位于:C:\Windows\System32\inetsrv\Microsoft.Web.Administration.dll
  5.  
  6. namespace Whir.Software.IISManager.IISManager
  7. {
  8. /// <summary>
  9. /// IIS应用程序池辅助类
  10. /// </summary>
  11. public class AppPoolService
  12. {
  13. protected static string Host = "localhost";
  14.  
  15. /// <summary>
  16. /// 取得所有应用程序池
  17. /// </summary>
  18. /// <returns></returns>
  19. public static List<string> GetAppPools()
  20. {
  21. var appPools = new DirectoryEntry(string.Format("IIS://{0}/W3SVC/AppPools", Host));
  22. return (from DirectoryEntry entry in appPools.Children select entry.Name).ToList();
  23. }
  24.  
  25. /// <summary>
  26. /// 取得单个应用程序池
  27. /// </summary>
  28. /// <returns></returns>
  29. public static ApplicationPool GetAppPool(string appPoolName)
  30. {
  31. ApplicationPool app = null;
  32. var appPools = new DirectoryEntry(string.Format("IIS://{0}/W3SVC/AppPools", Host));
  33. foreach (DirectoryEntry entry in appPools.Children)
  34. {
  35. if (entry.Name == appPoolName)
  36. {
  37. var manager = new ServerManager();
  38. app = manager.ApplicationPools[appPoolName];
  39. }
  40. }
  41. return app;
  42. }
  43.  
  44. /// <summary>
  45. /// 判断程序池是否存在
  46. /// </summary>
  47. /// <param name="appPoolName">程序池名称</param>
  48. /// <returns>true存在 false不存在</returns>
  49. public static bool IsAppPoolExsit(string appPoolName)
  50. {
  51. bool result = false;
  52. var appPools = new DirectoryEntry(string.Format("IIS://{0}/W3SVC/AppPools", Host));
  53. foreach (DirectoryEntry entry in appPools.Children)
  54. {
  55. if (entry.Name.Equals(appPoolName))
  56. {
  57. result = true;
  58. break;
  59. }
  60. }
  61. return result;
  62. }
  63.  
  64. /// <summary>
  65. /// 删除指定程序池
  66. /// </summary>
  67. /// <param name="appPoolName">程序池名称</param>
  68. /// <returns>true删除成功 false删除失败</returns>
  69. public static bool DeleteAppPool(string appPoolName)
  70. {
  71. bool result = false;
  72. var appPools = new DirectoryEntry(string.Format("IIS://{0}/W3SVC/AppPools", Host));
  73. foreach (DirectoryEntry entry in appPools.Children)
  74. {
  75. if (entry.Name.Equals(appPoolName))
  76. {
  77. try
  78. {
  79. entry.DeleteTree();
  80. result = true;
  81. break;
  82. }
  83. catch
  84. {
  85. result = false;
  86. }
  87. }
  88. }
  89. return result;
  90. }
  91.  
  92. /// <summary>
  93. /// 创建应用程序池
  94. /// </summary>
  95. /// <param name="appPool"></param>
  96. /// <returns></returns>
  97. public static bool CreateAppPool(string appPool)
  98. {
  99. try
  100. {
  101. if (!IsAppPoolExsit(appPool))
  102. {
  103. var appPools = new DirectoryEntry(string.Format("IIS://{0}/W3SVC/AppPools", Host));
  104. DirectoryEntry entry = appPools.Children.Add(appPool, "IIsApplicationPool");
  105. entry.CommitChanges();
  106. return true;
  107. }
  108. }
  109. catch
  110. {
  111. return false;
  112. }
  113. return false;
  114. }
  115.  
  116. /// <summary>
  117. /// 编辑应用程序池
  118. /// </summary>
  119. /// <param name="application"></param>
  120. /// <returns></returns>
  121. public static bool EditAppPool(ApplicationPool application)
  122. {
  123. try
  124. {
  125. if (IsAppPoolExsit(application.Name))
  126. {
  127. var manager = new ServerManager();
  128. manager.ApplicationPools[application.Name].ManagedRuntimeVersion = application.ManagedRuntimeVersion;
  129. manager.ApplicationPools[application.Name].ManagedPipelineMode = application.ManagedPipelineMode;
  130. //托管模式Integrated为集成 Classic为经典
  131. manager.CommitChanges();
  132. return true;
  133. }
  134. }
  135. catch
  136. {
  137. return false;
  138. }
  139. return false;
  140. }
  141. }
  142. }