Ejemplo n.º 1
0
        /// <summary>
        /// Applies the specified service list.
        /// </summary>
        /// <param name="serviceList">The service list.</param>
        public static void Apply(List <IWindowsServiceInfo> serviceList)
        {
            Hashtable      serviceTable = WindowsServiceManager.GetWindowsServicesTable();
            WindowsService temp;

            foreach (IWindowsServiceInfo item in serviceList)
            {
                temp = (WindowsService)serviceTable[item.ServiceName];

                if (temp != null)
                {
                    try
                    {
                        switch (item.ForcedStatus)
                        {
                        case ServiceForcedStatus.RUNNING:
                            if (temp.Status != ServiceControllerStatus.Running)
                            {
                                temp.Service.Start();
                            }
                            break;

                        case ServiceForcedStatus.STOPPED:
                            if (temp.Status != ServiceControllerStatus.Stopped)
                            {
                                temp.Service.Stop();
                            }
                            break;

                        default:
                            break;
                        }
                    }
                    catch (Exception)
                    {
                    }
                    finally { }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Forces the services.
        /// </summary>
        /// <param name="service">The service.</param>
        /// <param name="forcedStatus">The forced status.</param>
        public static void ForceServices(string[] service, ServiceForcedStatus forcedStatus)
        {
            Hashtable      serviceTable = WindowsServiceManager.GetWindowsServicesTable();
            WindowsService temp;

            foreach (string item in service)
            {
                temp = (WindowsService)serviceTable[item];

                if (temp != null)
                {
                    try
                    {
                        if ((temp.Status == ServiceControllerStatus.Running && forcedStatus == ServiceForcedStatus.RUNNING) ||
                            (temp.Status == ServiceControllerStatus.Stopped && forcedStatus == ServiceForcedStatus.STOPPED))
                        {
                            // non dobbiamo fare nulla
                            continue;
                        }

                        if (forcedStatus == ServiceForcedStatus.RUNNING)
                        {
                            temp.Service.Start();
                        }
                        else if (forcedStatus == ServiceForcedStatus.STOPPED)
                        {
                            temp.Service.Stop();
                        }
                    }
                    catch (Exception)
                    {
                    }
                    finally { }
                }
            }
        }