{"record":{"id":"f4ed0d9f83033067","repo":"OdysseusYuan/LKY_OfficeTools","slug":"serv-name-f4ed0d","errorCode":null,"errorMessage":"重启服务 {serv_name} 时失败。未找到该服务！","messagePattern":"重启服务 (.+?) 时失败。未找到该服务！","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"LKY_OfficeTools/Lib/Lib_AppServiceConfig.cs","lineNumber":258,"sourceCode":"            }\n            catch (Exception Ex)\n            {\n                new Log(Ex.ToString());\n                return false;\n            }\n        }\n\n        internal static void RestartSelf()\n        {\n            try\n            {\n                //自身服务名称\n                string serv_name = AppAttribute.ServiceName;\n\n                //未创建服务，不能停止！\n                if (!Query.IsCreated(serv_name))\n                {\n                    throw new Exception($\"重启服务 {serv_name} 时失败。未找到该服务！\");\n                }\n\n                //已安装服务，开始重启\n                string cmd = $\"(net stop {serv_name})&(net start {serv_name})\";\n                string result = Com_ExeOS.Run.Cmd(cmd);\n            }\n            catch (Exception Ex)\n            {\n                new Log(Ex.ToString());\n                return;\n            }\n        }\n    }\n}\n","sourceCodeStart":240,"sourceCodeEnd":273,"githubUrl":"https://github.com/OdysseusYuan/LKY_OfficeTools/blob/6f9a1bd471918f3310e6be4a6d7f42d0d6e467cf/LKY_OfficeTools/Lib/Lib_AppServiceConfig.cs#L240-L273","documentation":"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\".","triggerScenarios":"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.","commonSituations":"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\".","solutions":["Relaunch the application as Administrator — IsCreated returns false when it cannot enumerate services due to missing privileges.","Verify the service exists from an elevated shell: `sc query LKYOfficeToolsService`. If absent, run the app's service-creation step first.","Confirm the service name matches AppAttribute.ServiceName (\"LKYOfficeToolsService\"); a manual rename or DisplayName mismatch causes the lookup to miss.","If developing, add a privilege pre-check before IsCreated so the error message distinguishes \"not installed\" from \"not elevated\"."],"exampleFix":"// before\nif (!Query.IsCreated(serv_name))\n{\n    throw new Exception($\"重启服务 {serv_name} 时失败。未找到该服务！\");\n}\n\n// after — separate privilege failure from genuine absence\nvar principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());\nif (!principal.IsInRole(WindowsBuiltInRole.Administrator))\n{\n    throw new Exception($\"重启服务 {serv_name} 需要管理员权限。\");\n}\nif (!Query.IsCreated(serv_name))\n{\n    throw new Exception($\"服务 {serv_name} 未安装，请先执行服务创建步骤。\");\n}","handlingStrategy":"validation","validationCode":"// Run before RestartSelf: confirm elevation, then service presence.\nvar principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());\nif (!principal.IsInRole(WindowsBuiltInRole.Administrator))\n{\n    new Log(\"RestartSelf 需要 Administrator 权限，否则无法枚举服务。\");\n    return;\n}\nif (!Com_ServiceOS.Query.IsCreated(AppAttribute.ServiceName))\n{\n    new Log($\"服务 {AppAttribute.ServiceName} 未安装，请先执行服务创建步骤。\");\n    return;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always run the service-management entry points elevated; IsCreated silently returns false without admin rights.","Ensure the service-creation flow runs before any restart/stop call.","Do not manually rename the service; it must match AppAttribute.ServiceName (\"LKYOfficeToolsService\").","Use `sc query LKYOfficeToolsService` from an elevated shell to confirm state before debugging."],"tags":["windows-service","permissions","service-management","elevation"],"backgroundTag":null,"analyzedSha":"6f9a1bd471918f3310e6be4a6d7f42d0d6e467cf","analyzedAt":"2026-08-13T18:00:34.810Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}