IsServiceStopped() public static method

Determine whther the service with the given name is stopped.
public static IsServiceStopped ( string serviceName ) : bool
serviceName string The name of the service.
return bool
コード例 #1
0
        /// <summary>
        /// Uninstalls the service with the given name.
        /// </summary>
        /// <param name="name">The name of the service to uninstall.</param>
        /// <returns>True for success. Otherwise, false.</returns>
        public static bool UnInstallService(string name)
        {
            IntPtr svHandle = IntPtr.Zero;

            try
            {
                IntPtr scHandle = OpenSCManagerW(null, null, (uint)ServiceAccess.GenericWrite);

                if (scHandle.ToInt64() != 0)
                {
                    int DELETE = 0x10000;
                    svHandle = OpenServiceW(scHandle, name, DELETE);

                    if (svHandle.ToInt64() == 0)
                    {
                        Utils.Trace("OpenService Error: {0}", GetLastError());
                        return(false);
                    }

                    // stop the service.
                    bool running = !ServiceManager.IsServiceStopped(name);

                    if (running)
                    {
                        if (ServiceManager.StopService(name, new TimeSpan(0, 0, 0, 60)))
                        {
                            running = false;
                            Utils.Trace("{0} Service Stopped", name);
                        }
                    }

                    bool uninstalled = (DeleteService(svHandle) != 0);

                    if (uninstalled)
                    {
                        // If the service was running the delete marks the service
                        // for deletion. We need to stop the service to fully uninstall it.
                        if (running)
                        {
                            ServiceManager.StopService(name, new TimeSpan(0, 0, 0, 60));
                        }

                        return(true);
                    }

                    Utils.Trace("DeleteService Failed: {0}", name);
                    return(false);
                }
            }
            catch (Exception e)
            {
                Utils.Trace(e, "UnInstallService Exception");
            }
            finally
            {
                SafeCloseServiceHandle(svHandle);
            }

            return(false);
        }
コード例 #2
0
        /// <summary>
        /// Stops the service.
        /// </summary>
        /// <param name="serviceName">Name of the service.</param>
        /// <returns>True if stopped successfully.</returns>
        public static bool StopService(string serviceName)
        {
            IntPtr svHandle = IntPtr.Zero;

            try
            {
                IntPtr scHandle = OpenSCManagerW(null, null, (uint)ServiceAccess.GenericWrite);

                if (scHandle.ToInt64() != 0)
                {
                    svHandle = OpenServiceW(scHandle, serviceName, (int)ServiceAccess.AllAccess);

                    if (svHandle.ToInt64() == 0)
                    {
                        Utils.Trace("OpenService Error: {0}", GetLastError());
                        return(false);
                    }

                    // stop the service.
                    bool stopped = ServiceManager.IsServiceStopped(serviceName);

                    if (stopped)
                    {
                        if (ServiceManager.StopService(serviceName, new TimeSpan(0, 0, 0, 60)))
                        {
                            Utils.Trace("{0} Service Stopped", serviceName);
                            return(true);
                        }
                    }

                    return(false);
                }
            }
            catch (Exception e)
            {
                Utils.Trace(e, "Unexpected error stopping service.");
            }
            finally
            {
                SafeCloseServiceHandle(svHandle);
            }

            return(false);
        }