当前位置:   article > 正文

C# 操纵 IIS 类_c#实现对iis网站和应用程序池实时监测(网站停止后自动重启)

c#实现对iis网站和应用程序池实时监测(网站停止后自动重启)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.DirectoryServices;
  7. using System.Collections;
  8. namespace IIS_Controller.Class
  9. {
  10. public class ControlIIS
  11. {
  12. #region UserName,Password,HostName的定义
  13. public static string HostName
  14. {
  15. get
  16. {
  17. return hostName;
  18. }
  19. set
  20. {
  21. hostName = value;
  22. }
  23. }
  24. public static string UserName
  25. {
  26. get
  27. {
  28. return userName;
  29. }
  30. set
  31. {
  32. userName = value;
  33. }
  34. }
  35. public static string Password
  36. {
  37. get
  38. {
  39. return password;
  40. }
  41. set
  42. {
  43. if (UserName.Length <= 1)
  44. {
  45. throw new ArgumentException("还没有指定好用户名。请先指定用户名");
  46. }
  47. password = value;
  48. }
  49. }
  50. public static void RemoteConfig(string hostName, string userName, string password)
  51. {
  52. HostName = hostName;
  53. UserName = userName;
  54. Password = password;
  55. }
  56. private static string hostName = "localhost";
  57. private static string userName = "qf";
  58. private static string password = "qinfei";
  59. #endregion
  60. #region 根据路径构造Entry的方法
  61. /// <summary>
  62. /// 根据是否有用户名来判断是否是远程服务器。
  63. /// 然后再构造出不同的DirectoryEntry出来
  64. /// </summary>
  65. /// <param name="entPath">DirectoryEntry的路径</param>
  66. /// <returns>返回的是DirectoryEntry实例</returns>
  67. public static DirectoryEntry GetDirectoryEntry(string entPath)
  68. {
  69. DirectoryEntry ent;
  70. if (UserName == null)
  71. {
  72. ent = new DirectoryEntry(entPath);
  73. }
  74. else
  75. {
  76. ent = new DirectoryEntry(entPath, HostName + "\\" + UserName, Password, AuthenticationTypes.Secure);
  77. //ent = new DirectoryEntry(entPath, UserName, Password, AuthenticationTypes.Secure);
  78. }
  79. return ent;
  80. }
  81. #endregion
  82. #region 添加,删除网站的方法
  83. public static void CreateNewWebSite(string hostIP, string portNum, string descOfWebSite, string commentOfWebSite, string webPath)
  84. {
  85. if (!EnsureNewSiteEnavaible(hostIP + portNum + descOfWebSite))
  86. {
  87. throw new ArgumentNullException("已经有了这样的网站了。" + Environment.NewLine + hostIP + portNum + descOfWebSite);
  88. }
  89. string entPath = String.Format("IIS://{0}/w3svc", HostName);
  90. DirectoryEntry rootEntry = GetDirectoryEntry(entPath);//取得iis路径
  91. string newSiteNum = GetNewWebSiteID(); //取得新网站ID
  92. DirectoryEntry newSiteEntry = rootEntry.Children.Add(newSiteNum, "IIsWebServer"); //增加站点
  93. newSiteEntry.CommitChanges();//保存对区域的更改(这里对站点的更改)
  94. newSiteEntry.Properties["ServerBindings"].Value = hostIP + portNum + descOfWebSite;
  95. newSiteEntry.Properties["ServerComment"].Value = commentOfWebSite;
  96. newSiteEntry.CommitChanges();
  97. DirectoryEntry vdEntry = newSiteEntry.Children.Add("root", "IIsWebVirtualDir");
  98. vdEntry.CommitChanges();
  99. vdEntry.Properties["Path"].Value = webPath;
  100. vdEntry.CommitChanges();
  101. }
  102. /// <summary>
  103. /// 删除一个网站。根据网站名称删除。
  104. /// </summary>
  105. /// <param name="siteName">网站名称</param>
  106. public static void DeleteWebSiteByName(string siteName)
  107. {
  108. string siteNum = GetWebSiteNum(siteName);
  109. string siteEntPath = String.Format("IIS://{0}/w3svc/{1}", HostName, siteNum);
  110. DirectoryEntry siteEntry = GetDirectoryEntry(siteEntPath);
  111. string rootPath = String.Format("IIS://{0}/w3svc", HostName);
  112. DirectoryEntry rootEntry = GetDirectoryEntry(rootPath);
  113. rootEntry.Children.Remove(siteEntry);
  114. rootEntry.CommitChanges();
  115. }
  116. #endregion
  117. #region Start和Stop网站的方法
  118. public static void StartWebSite(string siteName)
  119. {
  120. string siteNum = GetWebSiteNum(siteName);
  121. string siteEntPath = String.Format("IIS://{0}/w3svc/{1}", HostName, siteNum);
  122. DirectoryEntry siteEntry = GetDirectoryEntry(siteEntPath);
  123. siteEntry.Invoke("Start", new object[] { });
  124. }
  125. public static void StopWebSite(string siteName)
  126. {
  127. string siteNum = GetWebSiteNum(siteName);
  128. string siteEntPath = String.Format("IIS://{0}/w3svc/{1}", HostName, siteNum);
  129. DirectoryEntry siteEntry = GetDirectoryEntry(siteEntPath);
  130. siteEntry.Invoke("Stop", new object[] { });
  131. }
  132. #endregion
  133. #region 确认网站是否相同
  134. /// <summary>
  135. /// 确定一个新的网站与现有的网站没有相同的。
  136. /// 这样防止将非法的数据存放到IIS里面去
  137. /// </summary>
  138. /// <param name="bindStr">网站邦定信息</param>
  139. /// <returns>真为可以创建,假为不可以创建</returns>
  140. public static bool EnsureNewSiteEnavaible(string bindStr)
  141. {
  142. string entPath = String.Format("IIS://{0}/w3svc", HostName);
  143. DirectoryEntry ent = GetDirectoryEntry(entPath);
  144. foreach (DirectoryEntry child in ent.Children)
  145. {
  146. if (child.SchemaClassName == "IIsWebServer")
  147. {
  148. if (child.Properties["ServerBindings"].Value != null)
  149. {
  150. if (child.Properties["ServerBindings"].Value.ToString() == bindStr)
  151. {
  152. return false;
  153. }
  154. }
  155. }
  156. }
  157. return true;
  158. }
  159. #endregion
  160. #region 获取一个网站编号//一个输入参数为站点描述
  161. /// <summary>
  162. /// 输入参数为 站点的描述名 默认是站点描述为 "默认网站"
  163. /// </summary>
  164. /// <param name="siteName">站点描述</param>
  165. /// <exception cref="NotFoundWebSiteException">表示没有找到网站</exception>
  166. public static string GetWebSiteNum(string siteName)
  167. {
  168. Regex regex = new Regex(siteName);
  169. string tmpStr;
  170. string entPath = String.Format("IIS://{0}/w3svc", HostName);
  171. DirectoryEntry ent = GetDirectoryEntry(entPath);
  172. foreach (DirectoryEntry child in ent.Children)
  173. {
  174. if (child.SchemaClassName == "IIsWebServer")
  175. {
  176. if (child.Properties["ServerBindings"].Value != null)
  177. {
  178. tmpStr = child.Properties["ServerBindings"].Value.ToString();
  179. if (regex.Match(tmpStr).Success)
  180. {
  181. return child.Name;
  182. }
  183. }
  184. if (child.Properties["ServerComment"].Value != null)
  185. {
  186. tmpStr = child.Properties["ServerComment"].Value.ToString();
  187. if (regex.Match(tmpStr).Success)
  188. {
  189. return child.Name;
  190. }
  191. }
  192. }
  193. }
  194. throw new Exception("没有找到我们想要的站点" + siteName);
  195. }
  196. #endregion
  197. #region 获取新网站id的方法
  198. /// <summary>
  199. /// 获取网站系统里面可以使用的最小的ID。
  200. /// 这是因为每个网站都需要有一个唯一的编号,而且这个编号越小越好。
  201. /// 这里面的算法经过了测试是没有问题的。
  202. /// </summary>
  203. /// <returns>最小的id</returns>
  204. public static string GetNewWebSiteID()
  205. {
  206. ArrayList list = new ArrayList();
  207. string tmpStr;
  208. string entPath = String.Format("IIS://{0}/w3svc", HostName);
  209. DirectoryEntry ent = GetDirectoryEntry(entPath);
  210. foreach (DirectoryEntry child in ent.Children)
  211. {
  212. if (child.SchemaClassName == "IIsWebServer")
  213. {
  214. tmpStr = child.Name.ToString();
  215. list.Add(Convert.ToInt32(tmpStr));
  216. }
  217. }
  218. list.Sort();
  219. int i = 1;
  220. foreach (int j in list)
  221. {
  222. if (i == j)
  223. {
  224. i++;
  225. }
  226. }
  227. return i.ToString();
  228. }
  229. #endregion
  230. #region 增加,删除主机头
  231. /// <summary>
  232. /// 增加主机头
  233. /// </summary>
  234. /// <param name="HostName">站点描述</param>
  235. /// <param name="ip">ip</param>
  236. /// <param name="port">端口</param>
  237. /// <param name="domain">域名</param>
  238. public static void AddHostHeader(string HostName, string ip, int port, string domain)//增加主机头(站点编号.ip.端口.域名)
  239. {
  240. DirectoryEntry site = new DirectoryEntry(String.Format("IIS://{0}/w3svc", HostName));
  241. PropertyValueCollection serverBindings = site.Properties["ServerBindings"];
  242. string headerStr = string.Format("{0}:{1}:{2}", ip, port, domain);
  243. if (!serverBindings.Contains(headerStr))
  244. {
  245. serverBindings.Add(headerStr);
  246. }
  247. site.CommitChanges();
  248. }
  249. /// <summary>
  250. /// 删除主机头
  251. /// </summary>
  252. /// <param name="HostName">站点描述</param>
  253. /// <param name="ip">ip</param>
  254. /// <param name="port">端口</param>
  255. /// <param name="domain">域名</param>
  256. public static void DeleteHostHeader(string HostName, string ip, int port, string domain)//删除主机头(站点编号.ip.端口.域名)
  257. {
  258. DirectoryEntry site = new DirectoryEntry(String.Format("IIS://{0}/w3svc", HostName));
  259. PropertyValueCollection serverBindings = site.Properties["ServerBindings"];
  260. string headerStr = string.Format("{0}:{1}:{2}", ip, port, domain);
  261. if (serverBindings.Contains(headerStr))
  262. {
  263. serverBindings.Remove(headerStr);
  264. }
  265. site.CommitChanges();
  266. }
  267. #endregion
  268. }
  269. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/252339
推荐阅读
相关标签
  

闽ICP备14008679号