public void CreateOrUpdateService(string serviceName, string displayName, string description, string binaryPath, Win32ServiceCredentials credentials, bool autoStart = false, bool startImmediately = false, ErrorSeverity errorSeverity = ErrorSeverity.Normal) { if (string.IsNullOrEmpty(binaryPath)) { throw new ArgumentException("Value cannot be null or empty.", nameof(binaryPath)); } if (string.IsNullOrEmpty(serviceName)) { throw new ArgumentException("Value cannot be null or empty.", nameof(serviceName)); } try { using (var mgr = ServiceControlManager.Connect(nativeInterop, machineName, databaseName, ServiceControlManagerAccessRights.All)) { ServiceHandle existingService; Win32Exception errorException; if (mgr.TryOpenService(serviceName, ServiceControlAccessRights.All, out existingService, out errorException)) { using (existingService) { DoUpdateService(displayName, description, binaryPath, credentials, autoStart, errorSeverity, existingService); } } else { if (errorException.NativeErrorCode == KnownWin32ErrorCoes.ERROR_SERVICE_DOES_NOT_EXIST) { DoCreateService(mgr, serviceName, displayName, description, binaryPath, credentials, autoStart, startImmediately, errorSeverity); } else { throw errorException; } } } } catch (DllNotFoundException dllException) { throw new PlatformNotSupportedException(nameof(Win32ServiceHost) + " is only supported on Windows with service management API set.", dllException); } }
public void DeleteService(string serviceName) { if (string.IsNullOrEmpty(serviceName)) { throw new ArgumentException("Value cannot be null or empty.", nameof(serviceName)); } try { using (var mgr = ServiceControlManager.Connect(nativeInterop, machineName, databaseName, ServiceControlManagerAccessRights.All)) { using (var svc = mgr.OpenService(serviceName, ServiceControlAccessRights.All)) { svc.Delete(); } } } catch (DllNotFoundException dllException) { throw new PlatformNotSupportedException(nameof(Win32ServiceHost) + " is only supported on Windows with service management API set.", dllException); } }
public void CreateService(string serviceName, string displayName, string description, string binaryPath, Win32ServiceCredentials credentials, bool autoStart = false, bool startImmediately = false, ErrorSeverity errorSeverity = ErrorSeverity.Normal) { if (string.IsNullOrEmpty(binaryPath)) { throw new ArgumentException("Value cannot be null or empty.", nameof(binaryPath)); } if (string.IsNullOrEmpty(serviceName)) { throw new ArgumentException("Value cannot be null or empty.", nameof(serviceName)); } try { using (var mgr = ServiceControlManager.Connect(nativeInterop, machineName, databaseName, ServiceControlManagerAccessRights.All)) { DoCreateService(mgr, serviceName, displayName, description, binaryPath, credentials, autoStart, startImmediately, errorSeverity); } } catch (DllNotFoundException dllException) { throw new PlatformNotSupportedException(nameof(Win32ServiceHost) + " is only supported on Windows with service management API set.", dllException); } }