/IISControl/IISControl/IISManager.cs

http://kidvn-lab.googlecode.com/ · C# · 401 lines · 246 code · 52 blank · 103 comment · 43 complexity · eb54baec804db9289293f1938aa4b00a MD5 · raw file

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.ServiceProcess;
  6. using System.Diagnostics;
  7. namespace IISControl
  8. {
  9. // Summary:
  10. // Indicates the current state of the service.
  11. public enum ServiceControllerStatusX
  12. {
  13. // Summary:
  14. // The service is not running. This corresponds to the Win32 SERVICE_STOPPED
  15. // constant, which is defined as 0x00000001.
  16. Stopped = 1,
  17. //
  18. // Summary:
  19. // The service is starting. This corresponds to the Win32 SERVICE_START_PENDING
  20. // constant, which is defined as 0x00000002.
  21. StartPending = 2,
  22. //
  23. // Summary:
  24. // The service is stopping. This corresponds to the Win32 SERVICE_STOP_PENDING
  25. // constant, which is defined as 0x00000003.
  26. StopPending = 3,
  27. //
  28. // Summary:
  29. // The service is running. This corresponds to the Win32 SERVICE_RUNNING constant,
  30. // which is defined as 0x00000004.
  31. Running = 4,
  32. //
  33. // Summary:
  34. // The service continue is pending. This corresponds to the Win32 SERVICE_CONTINUE_PENDING
  35. // constant, which is defined as 0x00000005.
  36. ContinuePending = 5,
  37. //
  38. // Summary:
  39. // The service pause is pending. This corresponds to the Win32 SERVICE_PAUSE_PENDING
  40. // constant, which is defined as 0x00000006.
  41. PausePending = 6,
  42. //
  43. // Summary:
  44. // The service is paused. This corresponds to the Win32 SERVICE_PAUSED constant,
  45. // which is defined as 0x00000007.
  46. Paused = 7,
  47. //
  48. // Summary:
  49. // The service is not installed.
  50. NotInstalled = 8,
  51. }
  52. /// <summary>
  53. /// An controller of IIS services.
  54. /// </summary>
  55. public class IISManager
  56. {
  57. #region VARIABLES
  58. /// <summary>
  59. /// Stores all services of IIS.
  60. /// </summary>
  61. protected Hashtable iisServices;
  62. /// <summary>
  63. /// This string is the parameter for enabled the backup service whenever it was disabled,
  64. /// by using SC tool in MS Windows. This string required 1 parameter: the name of the service.
  65. /// </summary>
  66. protected const string CMD_PARAMS_ENABLED_SERVICE = "config \"{0}\" start= auto";
  67. protected const int SERVICE_TIMEOUT = 200;
  68. public Hashtable CurrentServices
  69. {
  70. get { return iisServices; }
  71. }
  72. /// <summary>
  73. /// Contains the names of all IIS services.
  74. /// </summary>
  75. public class IISServicesNames
  76. {
  77. /// <summary>
  78. /// Gets the number of IIS services.
  79. /// </summary>
  80. public const int ServicesCount = 4;
  81. /// <summary>
  82. /// Name of IIS Amin service.
  83. /// </summary>
  84. public const string IIS_ADMIN = "IISADMIN";
  85. /// <summary>
  86. /// Name of World Wide Web Publishing service.
  87. /// </summary>
  88. public const string WWW_PUBLISHING = "W3SVC";
  89. /// <summary>
  90. /// Name of SMTP service.
  91. /// </summary>
  92. public const string SMTP = "SMTPSVC";
  93. /// <summary>
  94. /// Name of FTP service.
  95. /// </summary>
  96. public const string FTP_PUBLISHING = "MSFtpsvc";
  97. }
  98. /// <summary>
  99. /// Determines that all IIS services are running.
  100. /// </summary>
  101. public bool IsAllServicesRun
  102. {
  103. get
  104. {
  105. try
  106. {
  107. int iServicesRun = 0;
  108. foreach (ServiceController service in this.iisServices.Values)
  109. {
  110. if (service.Status == ServiceControllerStatus.Running
  111. || service.Status == ServiceControllerStatus.StartPending)
  112. iServicesRun++;
  113. }
  114. return iServicesRun == IISServicesNames.ServicesCount;
  115. }
  116. catch (Exception)
  117. {
  118. throw;
  119. }
  120. }
  121. }
  122. #endregion
  123. #region CONSTUCTOR
  124. public IISManager()
  125. {
  126. this.iisServices = new Hashtable();
  127. }
  128. #endregion
  129. #region CORE METHODS
  130. /// <summary>
  131. /// Check if the IIS services are installed in the system.
  132. /// </summary>
  133. /// <returns>TRUE if all services are installed, otherwise, return FALSE.</returns>
  134. public bool CheckServicesInstallation()
  135. {
  136. try
  137. {
  138. this.iisServices.Clear();
  139. ServiceController[] services = ServiceController.GetServices();
  140. //find BackupService
  141. foreach (ServiceController service in services)
  142. {
  143. switch (service.ServiceName)
  144. {
  145. case IISServicesNames.IIS_ADMIN:
  146. this.iisServices.Add(service.ServiceName, service);
  147. break;
  148. case IISServicesNames.WWW_PUBLISHING:
  149. this.iisServices.Add(service.ServiceName, service);
  150. break;
  151. case IISServicesNames.SMTP:
  152. this.iisServices.Add(service.ServiceName, service);
  153. break;
  154. case IISServicesNames.FTP_PUBLISHING:
  155. this.iisServices.Add(service.ServiceName, service);
  156. break;
  157. default:
  158. break;
  159. }
  160. if(this.iisServices.Count == IISServicesNames.ServicesCount)
  161. break;
  162. }
  163. return this.iisServices.Count > 0;
  164. }
  165. catch (Exception)
  166. {
  167. throw;
  168. }
  169. }
  170. /// <summary>
  171. /// Gets the number of services which are running.
  172. /// </summary>
  173. /// <returns>the number of services which are running.</returns>
  174. public int GetRunningServicesCount()
  175. {
  176. try
  177. {
  178. int iServicesRun = 0;
  179. foreach (ServiceController service in this.iisServices.Values)
  180. {
  181. if (service.Status == ServiceControllerStatus.Running
  182. || service.Status == ServiceControllerStatus.StartPending)
  183. iServicesRun++;
  184. }
  185. return iServicesRun;
  186. }
  187. catch (Exception)
  188. {
  189. throw;
  190. }
  191. }
  192. /// <summary>
  193. /// Gets the status of all services.
  194. /// </summary>
  195. /// <returns>A hash table that contains pairs of keys and value: keys are the names of all IIS services, ad the values are the associate status messages of them.</returns>
  196. public Hashtable GetServicesStatus()
  197. {
  198. try
  199. {
  200. Hashtable servicesMsgs = new Hashtable();
  201. foreach (string strServiceName in this.iisServices.Keys)
  202. {
  203. ServiceController service = this.iisServices[strServiceName] as ServiceController;
  204. servicesMsgs.Add(strServiceName, service.Status.ToString());
  205. }
  206. return servicesMsgs;
  207. }
  208. catch (Exception)
  209. {
  210. throw;
  211. }
  212. }
  213. /// <summary>
  214. /// Gets the status of a specify IIS service.
  215. /// </summary>
  216. /// <param name="serviceName">Name of the IIS service.</param>
  217. /// <returns>Status of the specify IIS service.</returns>
  218. public ServiceControllerStatusX GetServiceStatus(string serviceName)
  219. {
  220. try
  221. {
  222. if (!iisServices.Contains(serviceName))
  223. return ServiceControllerStatusX.NotInstalled;
  224. ServiceController iisService = this.iisServices[serviceName] as ServiceController;
  225. return (ServiceControllerStatusX)iisService.Status;
  226. }
  227. catch (Exception)
  228. {
  229. throw;
  230. }
  231. }
  232. /// <summary>
  233. /// Detects if the specify IIS Service is already running ot not.
  234. /// </summary>
  235. /// <param name="serviceName">Name of the IIS service.</param>
  236. /// <returns>True if the service is running or start pendding, otherwise, return false.</returns>
  237. public bool IsServiceRunning(string serviceName)
  238. {
  239. try
  240. {
  241. ServiceController iisService = this.iisServices[serviceName] as ServiceController;
  242. if (iisService == null
  243. || (iisService.Status != ServiceControllerStatus.StartPending
  244. && iisService.Status != ServiceControllerStatus.Running))
  245. return false;
  246. return true;
  247. }
  248. catch (Exception)
  249. {
  250. throw;
  251. }
  252. }
  253. /// <summary>
  254. /// Starts all IIS services.
  255. /// </summary>
  256. public void StartIISServices()
  257. {
  258. try
  259. {
  260. foreach (ServiceController service in this.iisServices.Values)
  261. {
  262. if (service.ServiceName == IISServicesNames.IIS_ADMIN)
  263. service.Refresh();
  264. if (service.Status != ServiceControllerStatus.StartPending
  265. && service.Status != ServiceControllerStatus.Running)
  266. {
  267. try
  268. {
  269. service.Start();
  270. }
  271. catch (InvalidOperationException)
  272. {
  273. //Try to start service by command line.
  274. Process.Start("SC", String.Format(CMD_PARAMS_ENABLED_SERVICE, service.ServiceName));
  275. //Delay for service's starting.
  276. System.Threading.Thread.Sleep(SERVICE_TIMEOUT);
  277. //Restart service again.
  278. service.Start();
  279. }
  280. }
  281. }
  282. }
  283. catch (Exception)
  284. {
  285. throw;
  286. }
  287. }
  288. /// <summary>
  289. /// Start a specify service in IIS modules.
  290. /// </summary>
  291. /// <param name="serviceName">Name of the service to be started.</param>
  292. public void StartService(string serviceName)
  293. {
  294. try
  295. {
  296. ServiceController iisService = this.iisServices[serviceName] as ServiceController;
  297. if (iisService == null
  298. || iisService.Status == ServiceControllerStatus.StartPending
  299. || iisService.Status == ServiceControllerStatus.Running)
  300. return;
  301. iisService.Start();
  302. }
  303. catch (Exception)
  304. {
  305. throw;
  306. }
  307. }
  308. /// <summary>
  309. /// Stops all IIS services.
  310. /// </summary>
  311. public void StopIISServices()
  312. {
  313. try
  314. {
  315. ServiceController IISAdminService = this.iisServices[IISServicesNames.IIS_ADMIN] as ServiceController;
  316. if (IISAdminService == null
  317. || IISAdminService.Status == ServiceControllerStatus.StopPending
  318. || IISAdminService.Status == ServiceControllerStatus.Stopped)
  319. return;
  320. IISAdminService.Stop();
  321. }
  322. catch (Exception)
  323. {
  324. throw;
  325. }
  326. }
  327. /// <summary>
  328. /// Stop a specify service in IIS modules.
  329. /// </summary>
  330. /// <param name="serviceName">Name of the service to be stopped.</param>
  331. public void StopService(string serviceName)
  332. {
  333. try
  334. {
  335. ServiceController iisService = this.iisServices[serviceName] as ServiceController;
  336. if (iisService == null
  337. || iisService.Status == ServiceControllerStatus.StopPending
  338. || iisService.Status == ServiceControllerStatus.Stopped)
  339. return;
  340. iisService.Stop();
  341. }
  342. catch (Exception)
  343. {
  344. throw;
  345. }
  346. }
  347. #endregion
  348. }
  349. }