public static void DeleteService(string serviceName) { var databaseHandle = ServiceUtil.OpenServiceDatabase(); try { ServiceUtil.DeleteService(databaseHandle, serviceName); } finally { SafeNativeMethods.CloseServiceHandle(databaseHandle); } // Not sure why this comes after the deletion, but .NET ServiceInstaller does this, so perhaps there's a good reason ServiceUtil.StopService(serviceName); }
/// <summary> /// Uninstalls all services in this process (i.e. unregisters them from the service manager). The current /// state of the services doesn't matter for this method - running services will be stopped first. /// </summary> public void Uninstall() { var databaseHandle = ServiceUtil.OpenServiceDatabase(); try { foreach (var service in Services) { ServiceUtil.DeleteService(databaseHandle, service.ServiceName); ServiceUtil.StopService(service.ServiceName); // Not sure why this comes after the deletion, but .NET ServiceInstaller does this, so perhaps there's a good reason } } finally { ServiceUtil.CloseServiceDatabase(databaseHandle); } }
/// <summary> /// Stops this service. Returns true if the service state has been verified as stopped, or false otherwise. /// Will wait up to 5 seconds for the service to stop. Does not throw any exceptions. Note: if the service /// is running under the service host, in "service mode", use <see cref="StopSelf"/> instead. /// </summary> public new bool Stop() { return(ServiceUtil.StopService(ServiceName)); }