/IISControl/IISControl/IISManager.cs
http://kidvn-lab.googlecode.com/ · C# · 401 lines · 246 code · 52 blank · 103 comment · 43 complexity · eb54baec804db9289293f1938aa4b00a MD5 · raw file
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Text;
- using System.ServiceProcess;
- using System.Diagnostics;
-
- namespace IISControl
- {
- // Summary:
- // Indicates the current state of the service.
- public enum ServiceControllerStatusX
- {
- // Summary:
- // The service is not running. This corresponds to the Win32 SERVICE_STOPPED
- // constant, which is defined as 0x00000001.
- Stopped = 1,
- //
- // Summary:
- // The service is starting. This corresponds to the Win32 SERVICE_START_PENDING
- // constant, which is defined as 0x00000002.
- StartPending = 2,
- //
- // Summary:
- // The service is stopping. This corresponds to the Win32 SERVICE_STOP_PENDING
- // constant, which is defined as 0x00000003.
- StopPending = 3,
- //
- // Summary:
- // The service is running. This corresponds to the Win32 SERVICE_RUNNING constant,
- // which is defined as 0x00000004.
- Running = 4,
- //
- // Summary:
- // The service continue is pending. This corresponds to the Win32 SERVICE_CONTINUE_PENDING
- // constant, which is defined as 0x00000005.
- ContinuePending = 5,
- //
- // Summary:
- // The service pause is pending. This corresponds to the Win32 SERVICE_PAUSE_PENDING
- // constant, which is defined as 0x00000006.
- PausePending = 6,
- //
- // Summary:
- // The service is paused. This corresponds to the Win32 SERVICE_PAUSED constant,
- // which is defined as 0x00000007.
- Paused = 7,
- //
- // Summary:
- // The service is not installed.
- NotInstalled = 8,
- }
-
- /// <summary>
- /// An controller of IIS services.
- /// </summary>
- public class IISManager
- {
- #region VARIABLES
-
- /// <summary>
- /// Stores all services of IIS.
- /// </summary>
- protected Hashtable iisServices;
-
- /// <summary>
- /// This string is the parameter for enabled the backup service whenever it was disabled,
- /// by using SC tool in MS Windows. This string required 1 parameter: the name of the service.
- /// </summary>
- protected const string CMD_PARAMS_ENABLED_SERVICE = "config \"{0}\" start= auto";
-
- protected const int SERVICE_TIMEOUT = 200;
-
- public Hashtable CurrentServices
- {
- get { return iisServices; }
- }
-
- /// <summary>
- /// Contains the names of all IIS services.
- /// </summary>
- public class IISServicesNames
- {
- /// <summary>
- /// Gets the number of IIS services.
- /// </summary>
- public const int ServicesCount = 4;
-
- /// <summary>
- /// Name of IIS Amin service.
- /// </summary>
- public const string IIS_ADMIN = "IISADMIN";
-
- /// <summary>
- /// Name of World Wide Web Publishing service.
- /// </summary>
- public const string WWW_PUBLISHING = "W3SVC";
-
- /// <summary>
- /// Name of SMTP service.
- /// </summary>
- public const string SMTP = "SMTPSVC";
-
- /// <summary>
- /// Name of FTP service.
- /// </summary>
- public const string FTP_PUBLISHING = "MSFtpsvc";
- }
-
- /// <summary>
- /// Determines that all IIS services are running.
- /// </summary>
- public bool IsAllServicesRun
- {
- get
- {
- try
- {
- int iServicesRun = 0;
-
- foreach (ServiceController service in this.iisServices.Values)
- {
- if (service.Status == ServiceControllerStatus.Running
- || service.Status == ServiceControllerStatus.StartPending)
- iServicesRun++;
- }
-
- return iServicesRun == IISServicesNames.ServicesCount;
- }
- catch (Exception)
- {
- throw;
- }
- }
- }
-
- #endregion
-
- #region CONSTUCTOR
-
- public IISManager()
- {
- this.iisServices = new Hashtable();
- }
-
- #endregion
-
- #region CORE METHODS
-
- /// <summary>
- /// Check if the IIS services are installed in the system.
- /// </summary>
- /// <returns>TRUE if all services are installed, otherwise, return FALSE.</returns>
- public bool CheckServicesInstallation()
- {
- try
- {
- this.iisServices.Clear();
- ServiceController[] services = ServiceController.GetServices();
-
- //find BackupService
- foreach (ServiceController service in services)
- {
- switch (service.ServiceName)
- {
- case IISServicesNames.IIS_ADMIN:
- this.iisServices.Add(service.ServiceName, service);
- break;
-
- case IISServicesNames.WWW_PUBLISHING:
- this.iisServices.Add(service.ServiceName, service);
- break;
-
- case IISServicesNames.SMTP:
- this.iisServices.Add(service.ServiceName, service);
- break;
-
- case IISServicesNames.FTP_PUBLISHING:
- this.iisServices.Add(service.ServiceName, service);
- break;
-
- default:
- break;
- }
-
- if(this.iisServices.Count == IISServicesNames.ServicesCount)
- break;
- }
-
- return this.iisServices.Count > 0;
- }
- catch (Exception)
- {
- throw;
- }
- }
-
- /// <summary>
- /// Gets the number of services which are running.
- /// </summary>
- /// <returns>the number of services which are running.</returns>
- public int GetRunningServicesCount()
- {
- try
- {
- int iServicesRun = 0;
-
- foreach (ServiceController service in this.iisServices.Values)
- {
- if (service.Status == ServiceControllerStatus.Running
- || service.Status == ServiceControllerStatus.StartPending)
- iServicesRun++;
- }
-
- return iServicesRun;
- }
- catch (Exception)
- {
- throw;
- }
- }
-
- /// <summary>
- /// Gets the status of all services.
- /// </summary>
- /// <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>
- public Hashtable GetServicesStatus()
- {
- try
- {
- Hashtable servicesMsgs = new Hashtable();
-
- foreach (string strServiceName in this.iisServices.Keys)
- {
- ServiceController service = this.iisServices[strServiceName] as ServiceController;
- servicesMsgs.Add(strServiceName, service.Status.ToString());
- }
-
- return servicesMsgs;
- }
- catch (Exception)
- {
- throw;
- }
- }
-
- /// <summary>
- /// Gets the status of a specify IIS service.
- /// </summary>
- /// <param name="serviceName">Name of the IIS service.</param>
- /// <returns>Status of the specify IIS service.</returns>
- public ServiceControllerStatusX GetServiceStatus(string serviceName)
- {
- try
- {
- if (!iisServices.Contains(serviceName))
- return ServiceControllerStatusX.NotInstalled;
-
- ServiceController iisService = this.iisServices[serviceName] as ServiceController;
-
- return (ServiceControllerStatusX)iisService.Status;
- }
- catch (Exception)
- {
- throw;
- }
- }
-
- /// <summary>
- /// Detects if the specify IIS Service is already running ot not.
- /// </summary>
- /// <param name="serviceName">Name of the IIS service.</param>
- /// <returns>True if the service is running or start pendding, otherwise, return false.</returns>
- public bool IsServiceRunning(string serviceName)
- {
- try
- {
- ServiceController iisService = this.iisServices[serviceName] as ServiceController;
-
- if (iisService == null
- || (iisService.Status != ServiceControllerStatus.StartPending
- && iisService.Status != ServiceControllerStatus.Running))
- return false;
-
- return true;
- }
- catch (Exception)
- {
- throw;
- }
- }
-
- /// <summary>
- /// Starts all IIS services.
- /// </summary>
- public void StartIISServices()
- {
- try
- {
- foreach (ServiceController service in this.iisServices.Values)
- {
- if (service.ServiceName == IISServicesNames.IIS_ADMIN)
- service.Refresh();
-
- if (service.Status != ServiceControllerStatus.StartPending
- && service.Status != ServiceControllerStatus.Running)
- {
- try
- {
- service.Start();
- }
- catch (InvalidOperationException)
- {
- //Try to start service by command line.
- Process.Start("SC", String.Format(CMD_PARAMS_ENABLED_SERVICE, service.ServiceName));
- //Delay for service's starting.
- System.Threading.Thread.Sleep(SERVICE_TIMEOUT);
-
- //Restart service again.
- service.Start();
- }
- }
- }
- }
- catch (Exception)
- {
- throw;
- }
- }
-
- /// <summary>
- /// Start a specify service in IIS modules.
- /// </summary>
- /// <param name="serviceName">Name of the service to be started.</param>
- public void StartService(string serviceName)
- {
- try
- {
- ServiceController iisService = this.iisServices[serviceName] as ServiceController;
-
- if (iisService == null
- || iisService.Status == ServiceControllerStatus.StartPending
- || iisService.Status == ServiceControllerStatus.Running)
- return;
-
- iisService.Start();
- }
- catch (Exception)
- {
- throw;
- }
- }
-
- /// <summary>
- /// Stops all IIS services.
- /// </summary>
- public void StopIISServices()
- {
- try
- {
- ServiceController IISAdminService = this.iisServices[IISServicesNames.IIS_ADMIN] as ServiceController;
-
- if (IISAdminService == null
- || IISAdminService.Status == ServiceControllerStatus.StopPending
- || IISAdminService.Status == ServiceControllerStatus.Stopped)
- return;
-
- IISAdminService.Stop();
- }
- catch (Exception)
- {
- throw;
- }
- }
-
- /// <summary>
- /// Stop a specify service in IIS modules.
- /// </summary>
- /// <param name="serviceName">Name of the service to be stopped.</param>
- public void StopService(string serviceName)
- {
- try
- {
- ServiceController iisService = this.iisServices[serviceName] as ServiceController;
-
- if (iisService == null
- || iisService.Status == ServiceControllerStatus.StopPending
- || iisService.Status == ServiceControllerStatus.Stopped)
- return;
-
- iisService.Stop();
- }
- catch (Exception)
- {
- throw;
- }
- }
-
- #endregion
- }
- }