PeanutButter
Loading...
Searching...
No Matches
PeanutButter.WindowsServiceManagement.WindowsServiceUtil Class Reference

The cross-environment WindowsServiceUtil, backed by commands to sc.exe. More...

Inheritance diagram for PeanutButter.WindowsServiceManagement.WindowsServiceUtil:
PeanutButter.WindowsServiceManagement.IWindowsServiceUtil

Public Member Functions

 WindowsServiceUtil (string serviceName)
 Used to query existing services.
 WindowsServiceUtil (string serviceName, string displayName, string commandline)
 Used to install a service, but still provides the same query interface.
void Refresh ()
 Force a refresh of the current service state.
void Start ()
 Start the service and wait for it to be running.
void Start (bool wait)
 Start the service Explicitly wait or not for it to start.
Parameters
wait

void Stop ()
 Stop the service and wait for it to be running.
void Stop (ControlOptions options)
 Stop the service with the provided control options.
  • when Force is provided, if the service doesn't stop within the allowed period, it will be killed.
Parameters
options

void Stop (bool wait)
 Start the service Explicitly wait or not for it to stop.
Parameters
wait

void Pause ()
 Pause the service, waiting for it to enter the paused state.
void Pause (bool wait)
 Pause the service Explicitly wait or not for it to pause.
void Continue ()
 Continue the service, waiting for it to enter the continued state.
void Continue (bool wait)
 Continue the service Explicitly wait or not for it to continue.
void ConfigureStartup (ServiceStartupTypes startupType)
 Configure the service's startup type.
Parameters
startupType

void Install ()
 Installs the service with automatic start. Will error if the service is already found by name.
void Install (ServiceStartupTypes startupType)
 Install with the provided startup type.
Parameters
startupType

void Uninstall ()
 Uninstalls the service. If the service is not found by name, does nothing.
void Uninstall (bool wait)
 Uninstalls the service. If the service is not found by name, does nothing. Will wait for the uninstallation to complete.
void Uninstall (ControlOptions controlOptions)
 More control over uninstallation.
Parameters
options

void InstallAndStart ()
 Installs the service as AutoStart and starts it, waiting for the service to report that it's running.
void InstallAndStart (bool wait)
 Install and start the service as AutoStarting, with control over whether to wait for the service to have properly started.
Parameters
wait

void InstallAndStart (ServiceStartupTypes startupType, bool wait)
 Install and start the service, with control over whether to wait for the service to have properly started.
Parameters
startupType
wait

void Disable ()
 Disable the service from running.
void SetAutomaticStart ()
 Set the service to automatically start with the host system.
void SetManualStart ()
 Set the service to require manual start.
KillServiceResult KillService ()
 Kills the service, if running.
Returns

Static Public Member Functions

static IWindowsServiceUtil GetServiceByPid (int pid)
 Attempt to load a WindowsServiceUtil by the pid of a running process.
static IWindowsServiceUtil GetServiceByPath (string path)
 Attempt to load a WindowsServiceUtil by a full path to the executable.

Static Public Attributes

const int DEFAULT_SERVICE_CONTROL_TIMEOUT_SECONDS = 30
 The default timeout for service control operations.
const int DEFAULT_POLL_INTERVAL_MILLISECONDS = 500
 The default interval for polling service status during blocking service control operations.

Properties

string ServiceName [get]
 The name of the service, usable from, eg 'net stop {name}'.
string DisplayName [get]
 The name displayed in the service manager (service.msc)
ServiceState State [get]
 The current state of this service.
ServiceStartupTypes StartupType = ServiceStartupTypes.Unknown [get]
 The startup type of the service.
ServiceState[] AllowedStates [get]
 The states which this service may transition to, from the current state.
bool IsDisabled [get]
 Whether or not the service is disabled -> this is a shortcut to check the startup type vs the disabled startup type.
bool IsInstalled [get]
 Whether or not the service is installed -> this is a shortcut to check startup type vs unknown.
bool IsRunning [get]
 Whether or not the service is running.
  • always false for an uninstalled service

bool IsPaused [get]
 Whether or not the service is paused.
  • always false for an uninstalled service

bool IsMarkedForDelete [get]
 Test if the service is marked for deletion.
bool IsStoppable [get]
 True if the service is in a stoppable state.
bool IsStartable [get]
 True if the service is in a startable state.
int ServicePID [get]
 The process id for a running process (0 when not running)
string ServiceExe [get]
 The path to the service executable.
string[] Arguments [get]
 The arguments that the service is started with.
string Commandline [get]
 The full commandline for this service.
int ServiceControlTimeoutSeconds = DEFAULT_SERVICE_CONTROL_TIMEOUT_SECONDS [get, set]
 The timeout, in seconds, whilst waiting for service control operations to complete. Override this value if you have a service which is particularly slow to respond to control requests, eg takes long to start up.
int PollIntervalMilliseconds = DEFAULT_POLL_INTERVAL_MILLISECONDS [get, set]
 The interval, in milliseconds, between polling service state whilst running a blocking control operation, to determine if the desired state has been reached.

Detailed Description

The cross-environment WindowsServiceUtil, backed by commands to sc.exe.

Constructor & Destructor Documentation

◆ WindowsServiceUtil() [1/2]

PeanutButter.WindowsServiceManagement.WindowsServiceUtil.WindowsServiceUtil ( string serviceName)

Used to query existing services.

Parameters
serviceName

◆ WindowsServiceUtil() [2/2]

PeanutButter.WindowsServiceManagement.WindowsServiceUtil.WindowsServiceUtil ( string serviceName,
string displayName,
string commandline )

Used to install a service, but still provides the same query interface.

Parameters
serviceName
displayName
commandline

Member Function Documentation

◆ ConfigureStartup()

void PeanutButter.WindowsServiceManagement.WindowsServiceUtil.ConfigureStartup ( ServiceStartupTypes startupType)

Configure the service's startup type.

Parameters
startupType

Implements PeanutButter.WindowsServiceManagement.IWindowsServiceUtil.

◆ Continue() [1/2]

void PeanutButter.WindowsServiceManagement.WindowsServiceUtil.Continue ( )

Continue the service, waiting for it to enter the continued state.

Implements PeanutButter.WindowsServiceManagement.IWindowsServiceUtil.

◆ Continue() [2/2]

void PeanutButter.WindowsServiceManagement.WindowsServiceUtil.Continue ( bool wait)

Continue the service Explicitly wait or not for it to continue.

Implements PeanutButter.WindowsServiceManagement.IWindowsServiceUtil.

◆ Disable()

void PeanutButter.WindowsServiceManagement.WindowsServiceUtil.Disable ( )

Disable the service from running.

Implements PeanutButter.WindowsServiceManagement.IWindowsServiceUtil.

◆ GetServiceByPath()

IWindowsServiceUtil PeanutButter.WindowsServiceManagement.WindowsServiceUtil.GetServiceByPath ( string path)
static

Attempt to load a WindowsServiceUtil by a full path to the executable.

Parameters
path
Returns
Exceptions
ArgumentException

◆ GetServiceByPid()

IWindowsServiceUtil PeanutButter.WindowsServiceManagement.WindowsServiceUtil.GetServiceByPid ( int pid)
static

Attempt to load a WindowsServiceUtil by the pid of a running process.

Parameters
pid
Returns

◆ Install() [1/2]

void PeanutButter.WindowsServiceManagement.WindowsServiceUtil.Install ( )

Installs the service with automatic start. Will error if the service is already found by name.

Implements PeanutButter.WindowsServiceManagement.IWindowsServiceUtil.

◆ Install() [2/2]

void PeanutButter.WindowsServiceManagement.WindowsServiceUtil.Install ( ServiceStartupTypes startupType)

Install with the provided startup type.

Parameters
startupType

Implements PeanutButter.WindowsServiceManagement.IWindowsServiceUtil.

◆ InstallAndStart() [1/3]

void PeanutButter.WindowsServiceManagement.WindowsServiceUtil.InstallAndStart ( )

Installs the service as AutoStart and starts it, waiting for the service to report that it's running.

Implements PeanutButter.WindowsServiceManagement.IWindowsServiceUtil.

◆ InstallAndStart() [2/3]

void PeanutButter.WindowsServiceManagement.WindowsServiceUtil.InstallAndStart ( bool wait)

Install and start the service as AutoStarting, with control over whether to wait for the service to have properly started.

Parameters
wait

Implements PeanutButter.WindowsServiceManagement.IWindowsServiceUtil.

◆ InstallAndStart() [3/3]

void PeanutButter.WindowsServiceManagement.WindowsServiceUtil.InstallAndStart ( ServiceStartupTypes startupType,
bool wait )

Install and start the service, with control over whether to wait for the service to have properly started.

Parameters
startupType
wait

Implements PeanutButter.WindowsServiceManagement.IWindowsServiceUtil.

◆ KillService()

KillServiceResult PeanutButter.WindowsServiceManagement.WindowsServiceUtil.KillService ( )

Kills the service, if running.

Returns

Implements PeanutButter.WindowsServiceManagement.IWindowsServiceUtil.

◆ Pause() [1/2]

void PeanutButter.WindowsServiceManagement.WindowsServiceUtil.Pause ( )

Pause the service, waiting for it to enter the paused state.

Implements PeanutButter.WindowsServiceManagement.IWindowsServiceUtil.

◆ Pause() [2/2]

void PeanutButter.WindowsServiceManagement.WindowsServiceUtil.Pause ( bool wait)

Pause the service Explicitly wait or not for it to pause.

Implements PeanutButter.WindowsServiceManagement.IWindowsServiceUtil.

◆ SetAutomaticStart()

void PeanutButter.WindowsServiceManagement.WindowsServiceUtil.SetAutomaticStart ( )

Set the service to automatically start with the host system.

Implements PeanutButter.WindowsServiceManagement.IWindowsServiceUtil.

◆ SetManualStart()

void PeanutButter.WindowsServiceManagement.WindowsServiceUtil.SetManualStart ( )

Set the service to require manual start.

Implements PeanutButter.WindowsServiceManagement.IWindowsServiceUtil.

◆ Start() [1/2]

void PeanutButter.WindowsServiceManagement.WindowsServiceUtil.Start ( )

Start the service and wait for it to be running.

Implements PeanutButter.WindowsServiceManagement.IWindowsServiceUtil.

◆ Start() [2/2]

void PeanutButter.WindowsServiceManagement.WindowsServiceUtil.Start ( bool wait)

Start the service Explicitly wait or not for it to start.

Parameters
wait

Implements PeanutButter.WindowsServiceManagement.IWindowsServiceUtil.

◆ Stop() [1/3]

void PeanutButter.WindowsServiceManagement.WindowsServiceUtil.Stop ( )

Stop the service and wait for it to be running.

Implements PeanutButter.WindowsServiceManagement.IWindowsServiceUtil.

◆ Stop() [2/3]

void PeanutButter.WindowsServiceManagement.WindowsServiceUtil.Stop ( bool wait)

Start the service Explicitly wait or not for it to stop.

Parameters
wait

Implements PeanutButter.WindowsServiceManagement.IWindowsServiceUtil.

◆ Stop() [3/3]

void PeanutButter.WindowsServiceManagement.WindowsServiceUtil.Stop ( ControlOptions options)

Stop the service with the provided control options.

  • when Force is provided, if the service doesn't stop within the allowed period, it will be killed.
Parameters
options

Implements PeanutButter.WindowsServiceManagement.IWindowsServiceUtil.

◆ Uninstall() [1/3]

void PeanutButter.WindowsServiceManagement.WindowsServiceUtil.Uninstall ( )

Uninstalls the service. If the service is not found by name, does nothing.

Implements PeanutButter.WindowsServiceManagement.IWindowsServiceUtil.

◆ Uninstall() [2/3]

void PeanutButter.WindowsServiceManagement.WindowsServiceUtil.Uninstall ( bool wait)

Uninstalls the service. If the service is not found by name, does nothing. Will wait for the uninstallation to complete.

Implements PeanutButter.WindowsServiceManagement.IWindowsServiceUtil.

◆ Uninstall() [3/3]

void PeanutButter.WindowsServiceManagement.WindowsServiceUtil.Uninstall ( ControlOptions controlOptions)

More control over uninstallation.

Parameters
options

Implements PeanutButter.WindowsServiceManagement.IWindowsServiceUtil.

Property Documentation

◆ Arguments

string [] PeanutButter.WindowsServiceManagement.WindowsServiceUtil.Arguments
get

The arguments that the service is started with.

Implements PeanutButter.WindowsServiceManagement.IWindowsServiceUtil.

◆ Commandline

string PeanutButter.WindowsServiceManagement.WindowsServiceUtil.Commandline
get

The full commandline for this service.

Implements PeanutButter.WindowsServiceManagement.IWindowsServiceUtil.

◆ DisplayName

string PeanutButter.WindowsServiceManagement.WindowsServiceUtil.DisplayName
get

The name displayed in the service manager (service.msc)

Implements PeanutButter.WindowsServiceManagement.IWindowsServiceUtil.

◆ IsDisabled

bool PeanutButter.WindowsServiceManagement.WindowsServiceUtil.IsDisabled
get

Whether or not the service is disabled -> this is a shortcut to check the startup type vs the disabled startup type.

Implements PeanutButter.WindowsServiceManagement.IWindowsServiceUtil.

◆ IsInstalled

bool PeanutButter.WindowsServiceManagement.WindowsServiceUtil.IsInstalled
get

Whether or not the service is installed -> this is a shortcut to check startup type vs unknown.

Implements PeanutButter.WindowsServiceManagement.IWindowsServiceUtil.

◆ IsMarkedForDelete

bool PeanutButter.WindowsServiceManagement.WindowsServiceUtil.IsMarkedForDelete
get

Test if the service is marked for deletion.

Implements PeanutButter.WindowsServiceManagement.IWindowsServiceUtil.

◆ IsPaused

bool PeanutButter.WindowsServiceManagement.WindowsServiceUtil.IsPaused
get

Whether or not the service is paused.

  • always false for an uninstalled service

Implements PeanutButter.WindowsServiceManagement.IWindowsServiceUtil.

◆ IsRunning

bool PeanutButter.WindowsServiceManagement.WindowsServiceUtil.IsRunning
get

Whether or not the service is running.

  • always false for an uninstalled service

Implements PeanutButter.WindowsServiceManagement.IWindowsServiceUtil.

◆ IsStartable

bool PeanutButter.WindowsServiceManagement.WindowsServiceUtil.IsStartable
get

True if the service is in a startable state.

Implements PeanutButter.WindowsServiceManagement.IWindowsServiceUtil.

◆ IsStoppable

bool PeanutButter.WindowsServiceManagement.WindowsServiceUtil.IsStoppable
get

True if the service is in a stoppable state.

Implements PeanutButter.WindowsServiceManagement.IWindowsServiceUtil.

◆ ServiceExe

string PeanutButter.WindowsServiceManagement.WindowsServiceUtil.ServiceExe
get

The path to the service executable.

Implements PeanutButter.WindowsServiceManagement.IWindowsServiceUtil.

◆ ServiceName

string PeanutButter.WindowsServiceManagement.WindowsServiceUtil.ServiceName
get

The name of the service, usable from, eg 'net stop {name}'.

Implements PeanutButter.WindowsServiceManagement.IWindowsServiceUtil.

◆ ServicePID

int PeanutButter.WindowsServiceManagement.WindowsServiceUtil.ServicePID
get

The process id for a running process (0 when not running)

Implements PeanutButter.WindowsServiceManagement.IWindowsServiceUtil.

◆ StartupType

ServiceStartupTypes PeanutButter.WindowsServiceManagement.WindowsServiceUtil.StartupType = ServiceStartupTypes.Unknown
get

The startup type of the service.

Implements PeanutButter.WindowsServiceManagement.IWindowsServiceUtil.

◆ State

ServiceState PeanutButter.WindowsServiceManagement.WindowsServiceUtil.State
get

The current state of this service.

Implements PeanutButter.WindowsServiceManagement.IWindowsServiceUtil.


The documentation for this class was generated from the following file:
  • source/Win32Service/PeanutButter.WindowsServiceManagement/WindowsServiceUtil.cs