{"record":{"id":"9f64b0b461d7738b","repo":"OdysseusYuan/LKY_OfficeTools","slug":"serv-name-9f64b0","errorCode":null,"errorMessage":"删除服务 {serv_name} 失败！","messagePattern":"删除服务 (.+?) 失败！","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"LKY_OfficeTools/Common/Com_ServiceOS.cs","lineNumber":361,"sourceCode":"                    }\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\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}\\\"\";","sourceCodeStart":343,"sourceCodeEnd":379,"githubUrl":"https://github.com/OdysseusYuan/LKY_OfficeTools/blob/6f9a1bd471918f3310e6be4a6d7f42d0d6e467cf/LKY_OfficeTools/Common/Com_ServiceOS.cs#L343-L379","documentation":"Thrown in Com_ServiceOS.Config.Delete when the captured stdout from `sc delete` does not contain the success markers 成功 or success (Com_ServiceOS.cs:355-362). This is the genuine sc-level failure path: sc ran and reported an error such as access-denied or service-marked-for-deletion. Delete has no try/catch, so the exception PROPAGATES.","triggerScenarios":"Calling Config.Delete while not elevated (sc reports 'Access is denied'); deleting a service already marked for deletion but still loaded; deleting a service with running dependents; localized Windows whose output the marker check misses (though English/zh-CN are covered).","commonSituations":"Uninstall running without admin rights; service host still alive so SCM defers deletion; dependent services still running; non-admin service-management tooling.","solutions":["Run the host process elevated; sc delete requires administrator privileges.","Ensure the service is fully stopped and its host process has exited before deleting.","Wrap Config.Delete in try/catch and surface the actual sc error text from the log.","If already marked for deletion, restart the SCM (reboot or `sc reset`) and retry."],"exampleFix":"// before\nCom_ServiceOS.Config.Delete(servName);\n\n// after\ntry { Com_ServiceOS.Config.Delete(servName); }\ncatch (Exception ex) when (ex.Message.Contains(\"删除服务\") && !ex.Message.Contains(\"返回值为空\"))\n{ /* elevate / stop dependents / reboot then retry */ }","handlingStrategy":"try-catch","validationCode":"// Ensure preconditions that sc delete needs\nif (!Com_ServiceOS.Query.IsCreated(servName)) { return; }\n// (caller should already be elevated before calling Delete)","typeGuard":null,"tryCatchPattern":"try\n{\n    Com_ServiceOS.Config.Delete(servName);\n}\ncatch (Exception ex) when (ex.Message.Contains(\"删除服务\") && !ex.Message.Contains(\"返回值为空\"))\n{\n    // Genuine sc rejection (often access-denied); elevate / stop dependents / reboot then retry.\n}","preventionTips":["Run elevated; sc delete requires administrator privileges.","Ensure the service is fully Stopped and its host process has exited before deleting.","Wrap Config.Delete in try/catch since it propagates.","If the service is marked-for-deletion, reboot or reset SCM before retrying."],"tags":["windows-service","scm","service-uninstall","access-denied","unhandled-exception"],"backgroundTag":null,"analyzedSha":"6f9a1bd471918f3310e6be4a6d7f42d0d6e467cf","analyzedAt":"2026-08-13T18:00:34.810Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}