mirror of
https://github.com/fergalmoran/DnsServer.git
synced 2026-01-05 00:04:42 +00:00
Added tray icon
This commit is contained in:
72
DnsServerTrayIcon/WindowsServiceController.cs
Normal file
72
DnsServerTrayIcon/WindowsServiceController.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
using System.Linq;
|
||||
using System.ServiceProcess;
|
||||
|
||||
namespace DnsServerTrayIcon
|
||||
{
|
||||
public class WindowsServiceController
|
||||
{
|
||||
private readonly string _serviceName;
|
||||
|
||||
public WindowsServiceController(string serviceName)
|
||||
{
|
||||
_serviceName = serviceName;
|
||||
}
|
||||
|
||||
public void Restart()
|
||||
{
|
||||
Stop();
|
||||
Start();
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var service = new ServiceController(_serviceName))
|
||||
{
|
||||
service.Stop();
|
||||
service.WaitForStatus(ServiceControllerStatus.Stopped);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{ }
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var service = new ServiceController(_serviceName))
|
||||
{
|
||||
service.Start();
|
||||
service.WaitForStatus(ServiceControllerStatus.Running);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{ }
|
||||
}
|
||||
|
||||
public bool IsRunning => Status == ServiceControllerStatus.Running;
|
||||
|
||||
public bool IsStopped => Status == ServiceControllerStatus.Stopped;
|
||||
|
||||
public ServiceControllerStatus Status
|
||||
{
|
||||
get
|
||||
{
|
||||
using (var service = new ServiceController(_serviceName))
|
||||
{
|
||||
return service.Status;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsInstalled
|
||||
{
|
||||
get
|
||||
{
|
||||
return ServiceController.GetServices().Any(s => s.ServiceName == _serviceName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user