/// <summary> /// Stops the service. /// </summary> /// <param name="serviceName">Name of the service.</param> /// <returns>True if stopped successfully.</returns> public static bool StartService(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 running = ServiceManager.IsServiceRunning(serviceName); if (!running) { if (ServiceManager.StartService(serviceName, new TimeSpan(0, 0, 0, 60))) { Utils.Trace("{0} Service Started", serviceName); return(true); } } return(false); } } catch (Exception e) { Utils.Trace(e, "Unexpected error stopping service."); } finally { SafeCloseServiceHandle(svHandle); } return(false); }