{"record":{"id":"2fe3c7ffe43dc637","repo":"OdysseusYuan/LKY_OfficeTools","slug":"serv-name-2fe3c7","errorCode":null,"errorMessage":"删除服务 {serv_name} 时，返回值为空！","messagePattern":"删除服务 (.+?) 时，返回值为空！","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"LKY_OfficeTools/Common/Com_ServiceOS.cs","lineNumber":351,"sourceCode":"            internal static bool Delete(string serv_name)\n            {\n                if (Query.IsCreated(serv_name))\n                {\n                    //已经创建服务，才能删除\n\n                    //先停止服务\n                    if (!Action.Stop(serv_name))\n                    {\n                        throw new Exception($\"服务 {serv_name} 因无法停止，导致卸载失败！\");\n                    }\n\n                    string cmd_del = $\"sc delete \\\"{serv_name}\\\"\";\n                    var del_result = Com_ExeOS.Run.Cmd(cmd_del);\n\n                    //非空判断，若返回值为空，则为假\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","sourceCodeStart":333,"sourceCodeEnd":369,"githubUrl":"https://github.com/OdysseusYuan/LKY_OfficeTools/blob/6f9a1bd471918f3310e6be4a6d7f42d0d6e467cf/LKY_OfficeTools/Common/Com_ServiceOS.cs#L333-L369","documentation":"Thrown in Com_ServiceOS.Config.Delete when the stdout captured from `sc delete` via Com_ExeOS.Run.Cmd is null or empty (Com_ServiceOS.cs:349-351). Because Run.Cmd returns null only from its catch, this signals a cmd.exe/redirect infrastructure failure rather than an sc error. Delete has no try/catch, so the exception PROPAGATES to the caller.","triggerScenarios":"Run.Cmd throws because cmd.exe cannot launch, output redirection fails, or the process is terminated before writing stdout. A genuine sc failure (access denied / not found) still returns non-empty text and would hit error 7 instead.","commonSituations":"cmd.exe blocked by AppLocker/WDAC; stripped Windows image without cmd; stdio handle exhaustion under heavy process spawning; security software intercepting redirected console output.","solutions":["Wrap Config.Delete in try/catch (Delete does not catch its own throws).","Ensure cmd.exe is on PATH and not blocked by policy; run elevated.","Run `sc delete <name>` directly in a console to confirm sc itself works.","Check Lib_AppLog for the exception captured by Run.Cmd's catch for the real cause."],"exampleFix":"// before\nCom_ServiceOS.Config.Delete(servName);\n\n// after\ntry { Com_ServiceOS.Config.Delete(servName); }\ncatch (Exception ex) { /* log ex; fall back to `sc delete` via a shell if Cmd infrastructure failed */ }","handlingStrategy":"try-catch","validationCode":"if (string.IsNullOrEmpty(Com_ExeOS.Run.Cmd(\"ver\"))) { /* cmd broken; fix env */ return; }","typeGuard":null,"tryCatchPattern":"try\n{\n    Com_ServiceOS.Config.Delete(servName);\n}\ncatch (Exception ex) when (ex.Message.Contains(\"返回值为空\"))\n{\n    // Cmd infrastructure failed; fall back to running `sc delete` via another shell.\n}","preventionTips":["Wrap Config.Delete in try/catch; it propagates.","Ensure cmd.exe is available and not blocked by AppLocker/WDAC.","Run elevated.","Inspect Lib_AppLog for the exception captured in Run.Cmd's catch."],"tags":["windows-service","scm","cmd","service-uninstall","unhandled-exception"],"backgroundTag":null,"analyzedSha":"6f9a1bd471918f3310e6be4a6d7f42d0d6e467cf","analyzedAt":"2026-08-13T18:00:34.810Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}