OdysseusYuan/LKY_OfficeTools · error · Exception

删除服务 {serv_name} 失败!

Error message

删除服务 {serv_name} 失败!

What it means

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.

Source

Thrown at LKY_OfficeTools/Common/Com_ServiceOS.cs:361

                    }

                    string cmd_del = $"sc delete \"{serv_name}\"";
                    var del_result = Com_ExeOS.Run.Cmd(cmd_del);

                    //非空判断,若返回值为空,则为假
                    if (string.IsNullOrEmpty(del_result))
                    {
                        throw new Exception($"删除服务 {serv_name} 时,返回值为空!");
                    }

                    //判断是否删除成功
                    if (del_result.Contains("成功") || del_result.ToLower().Contains("success"))
                    {
                        return true;
                    }
                    else
                    {
                        throw new Exception($"删除服务 {serv_name} 失败!");
                    }
                }
                else
                {
                    throw new Exception($"没有找到名称为 {serv_name} 的服务,无法删除该服务!");
                }
            }

            internal class Modify
            {
                internal static bool BinPath(string serv_name, string serv_binpath)
                {
                    try
                    {
                        if (Query.IsCreated(serv_name))
                        {
                            //已经创建服务,才进行修改
                            string cmd_modify_binpath = $"sc config \"{serv_name}\" binPath=\"{serv_binpath}\"";

View on GitHub (pinned to 6f9a1bd471)

Solutions

  1. Run the host process elevated; sc delete requires administrator privileges.
  2. Ensure the service is fully stopped and its host process has exited before deleting.
  3. Wrap Config.Delete in try/catch and surface the actual sc error text from the log.
  4. If already marked for deletion, restart the SCM (reboot or `sc reset`) and retry.

Example fix

// before
Com_ServiceOS.Config.Delete(servName);

// after
try { Com_ServiceOS.Config.Delete(servName); }
catch (Exception ex) when (ex.Message.Contains("删除服务") && !ex.Message.Contains("返回值为空"))
{ /* elevate / stop dependents / reboot then retry */ }
Defensive patterns

Strategy: try-catch

Validate before calling

// Ensure preconditions that sc delete needs
if (!Com_ServiceOS.Query.IsCreated(servName)) { return; }
// (caller should already be elevated before calling Delete)

Try / catch

try
{
    Com_ServiceOS.Config.Delete(servName);
}
catch (Exception ex) when (ex.Message.Contains("删除服务") && !ex.Message.Contains("返回值为空"))
{
    // Genuine sc rejection (often access-denied); elevate / stop dependents / reboot then retry.
}

Prevention

When it happens

Trigger: 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).

Common situations: Uninstall running without admin rights; service host still alive so SCM defers deletion; dependent services still running; non-admin service-management tooling.

Related errors


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