OdysseusYuan/LKY_OfficeTools · error · Exception

重启服务 {serv_name} 时失败。未找到该服务!

Error message

重启服务 {serv_name} 时失败。未找到该服务!

What it means

Thrown by RestartSelf() when the app's own Windows service (named "LKYOfficeToolsService", derived from AppAttribute.ServiceName) cannot be located. The check uses Com_ServiceOS.Query.IsCreated, which returns false both when the service is genuinely not installed and when ServiceController.GetServices() throws due to insufficient privileges — the exception is swallowed and mapped to false. So this error conflates "service missing" with "not running elevated".

Source

Thrown at LKY_OfficeTools/Lib/Lib_AppServiceConfig.cs:258

            }
            catch (Exception Ex)
            {
                new Log(Ex.ToString());
                return false;
            }
        }

        internal static void RestartSelf()
        {
            try
            {
                //自身服务名称
                string serv_name = AppAttribute.ServiceName;

                //未创建服务,不能停止!
                if (!Query.IsCreated(serv_name))
                {
                    throw new Exception($"重启服务 {serv_name} 时失败。未找到该服务!");
                }

                //已安装服务,开始重启
                string cmd = $"(net stop {serv_name})&(net start {serv_name})";
                string result = Com_ExeOS.Run.Cmd(cmd);
            }
            catch (Exception Ex)
            {
                new Log(Ex.ToString());
                return;
            }
        }
    }
}

View on GitHub (pinned to 6f9a1bd471)

Solutions

  1. Relaunch the application as Administrator — IsCreated returns false when it cannot enumerate services due to missing privileges.
  2. Verify the service exists from an elevated shell: `sc query LKYOfficeToolsService`. If absent, run the app's service-creation step first.
  3. Confirm the service name matches AppAttribute.ServiceName ("LKYOfficeToolsService"); a manual rename or DisplayName mismatch causes the lookup to miss.
  4. If developing, add a privilege pre-check before IsCreated so the error message distinguishes "not installed" from "not elevated".

Example fix

// before
if (!Query.IsCreated(serv_name))
{
    throw new Exception($"重启服务 {serv_name} 时失败。未找到该服务!");
}

// after — separate privilege failure from genuine absence
var principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
if (!principal.IsInRole(WindowsBuiltInRole.Administrator))
{
    throw new Exception($"重启服务 {serv_name} 需要管理员权限。");
}
if (!Query.IsCreated(serv_name))
{
    throw new Exception($"服务 {serv_name} 未安装,请先执行服务创建步骤。");
}
Defensive patterns

Strategy: validation

Validate before calling

// Run before RestartSelf: confirm elevation, then service presence.
var principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
if (!principal.IsInRole(WindowsBuiltInRole.Administrator))
{
    new Log("RestartSelf 需要 Administrator 权限,否则无法枚举服务。");
    return;
}
if (!Com_ServiceOS.Query.IsCreated(AppAttribute.ServiceName))
{
    new Log($"服务 {AppAttribute.ServiceName} 未安装,请先执行服务创建步骤。");
    return;
}

Prevention

When it happens

Trigger: Calling RestartSelf() when (a) the service was never created by the app's service-installation step, (b) the service was manually uninstalled or renamed, or (c) the process runs without Administrator rights so GetServices() throws Win32Exception, which IsCreated catches and returns false.

Common situations: Running the tool interactively without elevation; executing RestartSelf before the service-creation flow completed; a prior uninstall removed the service; antivirus or another tool interfering with service enumeration; service registered under a different name than the computed "LKYOfficeToolsService".

Related errors


AI-assisted analysis of OdysseusYuan/LKY_OfficeTools@6f9a1bd471 (2026-08-13). Data as JSON: /api/errors/f4ed0d9f83033067. Report an issue: GitHub.