using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.ServiceProcess;
using System.Management;
namespace chc.servicemanagertray
{///
/// Decorates the ServiceController.
///
internal class ServiceWrapper
{
///
/// Favoroite field, fovaorites are displayed on the context menu of the notify icon.
///
private bool favorite = false;
///
/// Consturcts a new service wrapper.
///
///
///
public ServiceWrapper(ServiceController service, bool isFavorite)
{
this.ServiceController = service;
this.favorite = isFavorite;
LoadDescription();
}
private void LoadDescription()
{
ManagementObject wmiService;
wmiService = new ManagementObject("Win32_Service.Name='" + ServiceController.ServiceName + "'");
wmiService.Get();
Description = wmiService["Description"] == null ? "" : wmiService["Description"].ToString();
}
///
/// The description of the service in the registry
///
public string Description
{
get;
private set;
}
///
/// Property for favrotie.
///
public bool IsFavorite
{
get
{
return this.favorite;
}
set
{
if (!value)
{
Properties.Settings.Default.Favorites.Remove(this.ServiceController.ServiceName);
}
else
{
Properties.Settings.Default.Favorites.Add(this.ServiceController.ServiceName);
}
favorite = value;
}
}
///
/// Property for is running, changing the state stops/starts the service.
///
public bool IsRunning
{
get
{
return this.ServiceController.Status == ServiceControllerStatus.Running;
}
set
{
if (value)
{
this.Start();
}
else
{
this.Stop();
}
}
}
///
/// Starts, stops the service.
///
public void Start()
{
if (!ServiceManagerForm.IsElevated)
{
ServiceManagerForm.RequestElevatation(ServiceController.ServiceName, true);
return;
}
ServiceController.Refresh();
if (ServiceController.Status == ServiceControllerStatus.Stopped)
{
ServiceController.Start();
}
else
{
}
}
///
/// Stops the service.
///
public void Stop()
{
if (!ServiceManagerForm.IsElevated)
{
ServiceManagerForm.RequestElevatation(ServiceController.ServiceName, false);
return;
}
ServiceController.Refresh();
if (ServiceController.Status == ServiceControllerStatus.Running)
{
ServiceController.Stop();
}
}
///
/// Display name
///
public string DisplayName
{
get
{
return this.ServiceController.DisplayName;
}
}
///
/// Status
///
public ServiceControllerStatus Status
{
get
{
return this.ServiceController.Status;
}
}
///
/// The service controllers.
///
public ServiceController ServiceController
{
get;
private set;
}
}
}