using System.Diagnostics;
using System.ServiceProcess;
using SpdUp.Core;
using SpdUp.Models;
namespace SpdUp.Services;
///
/// Service management: list, stop, start Windows services.
///
public static class ServiceManagementService
{
///
/// Non-critical services commonly stopped for gaming.
///
public static readonly Dictionary KnownNonCriticalServices = new()
{
["SysMain"] = "Superfetch / SysMain – pre-loads apps into RAM",
["WSearch"] = "Windows Search indexing",
["DiagTrack"] = "Connected User Experiences and Telemetry",
["TabletInputService"] = "Touch Keyboard and Handwriting Panel",
["MapsBroker"] = "Downloaded Maps Manager",
["PcaSvc"] = "Program Compatibility Assistant",
["wisvc"] = "Windows Insider Service",
["WbioSrvc"] = "Windows Biometric Service",
["Fax"] = "Fax service",
["XblAuthManager"] = "Xbox Live Auth Manager",
["XblGameSave"] = "Xbox Live Game Save",
["XboxNetApiSvc"] = "Xbox Live Networking Service",
};
public static List GetServiceList(IEnumerable serviceNames)
{
var entries = new List();
foreach (var name in serviceNames)
{
try
{
using var sc = new ServiceController(name);
entries.Add(new ServiceEntry
{
ServiceName = sc.ServiceName,
DisplayName = sc.DisplayName,
Status = sc.Status.ToString(),
Description = KnownNonCriticalServices.GetValueOrDefault(name, "")
});
}
catch
{
entries.Add(new ServiceEntry
{
ServiceName = name,
DisplayName = name,
Status = "NotFound"
});
}
}
return entries;
}
public static bool StopService(string serviceName)
{
try
{
using var sc = new ServiceController(serviceName);
if (sc.Status == ServiceControllerStatus.Running)
{
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(15));
Logger.Info($"Stopped service: {serviceName}");
return true;
}
}
catch (Exception ex) { Logger.Error($"Failed to stop {serviceName}", ex); }
return false;
}
public static bool StartService(string serviceName)
{
try
{
using var sc = new ServiceController(serviceName);
if (sc.Status == ServiceControllerStatus.Stopped)
{
sc.Start();
sc.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(15));
Logger.Info($"Started service: {serviceName}");
return true;
}
}
catch (Exception ex) { Logger.Error($"Failed to start {serviceName}", ex); }
return false;
}
}