Add refresh command

This commit is contained in:
NextTurn
2020-07-23 00:00:00 +08:00
committed by Next Turn
parent 80323eaab8
commit 40b566d330
3 changed files with 83 additions and 1 deletions

View File

@@ -159,6 +159,10 @@ namespace WinSW
TestWait();
return;
case "refresh":
Refresh();
return;
case "help":
case "--help":
case "-h":
@@ -642,6 +646,48 @@ namespace WinSW
wsvc.RaiseOnStop();
}
void Refresh()
{
if (!elevated)
{
Elevate();
return;
}
using ServiceManager scm = ServiceManager.Open();
try
{
using Service sc = scm.OpenService(descriptor.Id);
sc.ChangeConfig(descriptor.Caption, descriptor.StartMode);
sc.SetDescription(descriptor.Description);
SC_ACTION[] actions = descriptor.FailureActions;
if (actions.Length > 0)
{
sc.SetFailureActions(descriptor.ResetFailureAfter, actions);
}
bool isDelayedAutoStart = descriptor.StartMode == ServiceStartMode.Automatic && descriptor.DelayedAutoStart;
if (isDelayedAutoStart)
{
sc.SetDelayedAutoStart(true);
}
string? securityDescriptor = descriptor.SecurityDescriptor;
if (securityDescriptor != null)
{
// throws ArgumentException
sc.SetSecurityDescriptor(new RawSecurityDescriptor(securityDescriptor));
}
}
catch (CommandException e) when (e.InnerException is Win32Exception inner && inner.NativeErrorCode == Errors.ERROR_SERVICE_DOES_NOT_EXIST)
{
ThrowNoSuchService(inner);
}
}
// [DoesNotReturn]
void Elevate()
{