{"record":{"id":"72929cc2c479f338","repo":"OdysseusYuan/LKY_OfficeTools","slug":"serv-name-72929c","errorCode":null,"errorMessage":"没有找到名称为 {serv_name} 的服务，无法删除该服务！","messagePattern":"没有找到名称为 (.+?) 的服务，无法删除该服务！","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"warning","filePath":"LKY_OfficeTools/Common/Com_ServiceOS.cs","lineNumber":366,"sourceCode":"                    //非空判断，若返回值为空，则为假\n                    if (string.IsNullOrEmpty(del_result))\n                    {\n                        throw new Exception($\"删除服务 {serv_name} 时，返回值为空！\");\n                    }\n\n                    //判断是否删除成功\n                    if (del_result.Contains(\"成功\") || del_result.ToLower().Contains(\"success\"))\n                    {\n                        return true;\n                    }\n                    else\n                    {\n                        throw new Exception($\"删除服务 {serv_name} 失败！\");\n                    }\n                }\n                else\n                {\n                    throw new Exception($\"没有找到名称为 {serv_name} 的服务，无法删除该服务！\");\n                }\n            }\n\n            internal class Modify\n            {\n                internal static bool BinPath(string serv_name, string serv_binpath)\n                {\n                    try\n                    {\n                        if (Query.IsCreated(serv_name))\n                        {\n                            //已经创建服务，才进行修改\n                            string cmd_modify_binpath = $\"sc config \\\"{serv_name}\\\" binPath=\\\"{serv_binpath}\\\"\";\n                            var modify_result = Com_ExeOS.Run.Cmd(cmd_modify_binpath);\n\n                            //非空判断，若返回值为空，则为假\n                            if (string.IsNullOrEmpty(modify_result))\n                            {","sourceCodeStart":348,"sourceCodeEnd":384,"githubUrl":"https://github.com/OdysseusYuan/LKY_OfficeTools/blob/6f9a1bd471918f3310e6be4a6d7f42d0d6e467cf/LKY_OfficeTools/Common/Com_ServiceOS.cs#L348-L384","documentation":"Thrown by Com_ServiceOS.Config.Delete in its else-branch when Query.IsCreated(serv_name) is false at entry (Com_ServiceOS.cs:364-367). The guard prevents issuing `sc delete` for a service that is not registered. Delete has no try/catch, so the exception PROPAGATES to the caller.","triggerScenarios":"Calling Config.Delete for a service that was never installed, was already deleted, or whose name does not case-insensitively match any ServiceController.GetServices() entry; also when GetServices throws so IsCreated returns false from its catch.","commonSituations":"Idempotent uninstall re-running after a prior successful delete; wrong service name (e.g. DisplayName used instead of ServiceName); cleanup on a machine where the product was never installed.","solutions":["Pre-check Query.IsCreated and treat absence as success for idempotent uninstalls.","Wrap Config.Delete in try/catch since Delete propagates.","Confirm the ServiceName via `sc query` before attempting deletion.","Skip the call entirely when the service is known not to exist."],"exampleFix":"// before\nCom_ServiceOS.Config.Delete(servName);\n\n// after\nif (Com_ServiceOS.Query.IsCreated(servName))\n    Com_ServiceOS.Config.Delete(servName);\n// else: nothing to delete; treat as success","handlingStrategy":"validation","validationCode":"if (Com_ServiceOS.Query.IsCreated(servName))\n    Com_ServiceOS.Config.Delete(servName);\n// else: nothing to delete; treat as success (idempotent)","typeGuard":"static bool ServiceRegistered(string name) => Com_ServiceOS.Query.IsCreated(name);","tryCatchPattern":"try { Com_ServiceOS.Config.Delete(servName); }\ncatch (Exception ex) when (ex.Message.Contains(\"无法删除该服务\")) { /* already gone; ignore */ }","preventionTips":["Make uninstall idempotent: skip Delete when IsCreated is false.","Remember Delete still propagates other throws, so also wrap it in try/catch.","Confirm ServiceName with `sc query` before deleting.","Order cleanup so it tolerates a service that was already removed."],"tags":["windows-service","scm","service-uninstall","not-found"],"backgroundTag":null,"analyzedSha":"6f9a1bd471918f3310e6be4a6d7f42d0d6e467cf","analyzedAt":"2026-08-13T18:00:34.810Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}