OdysseusYuan/LKY_OfficeTools · warning · Exception

没有找到名称为 {serv_name} 的服务,无法删除该服务!

Error message

没有找到名称为 {serv_name} 的服务,无法删除该服务!

What it means

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.

Source

Thrown at LKY_OfficeTools/Common/Com_ServiceOS.cs:366

                    //非空判断,若返回值为空,则为假
                    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}\"";
                            var modify_result = Com_ExeOS.Run.Cmd(cmd_modify_binpath);

                            //非空判断,若返回值为空,则为假
                            if (string.IsNullOrEmpty(modify_result))
                            {

View on GitHub (pinned to 6f9a1bd471)

Solutions

  1. Pre-check Query.IsCreated and treat absence as success for idempotent uninstalls.
  2. Wrap Config.Delete in try/catch since Delete propagates.
  3. Confirm the ServiceName via `sc query` before attempting deletion.
  4. Skip the call entirely when the service is known not to exist.

Example fix

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

// after
if (Com_ServiceOS.Query.IsCreated(servName))
    Com_ServiceOS.Config.Delete(servName);
// else: nothing to delete; treat as success
Defensive patterns

Strategy: validation

Validate before calling

if (Com_ServiceOS.Query.IsCreated(servName))
    Com_ServiceOS.Config.Delete(servName);
// else: nothing to delete; treat as success (idempotent)

Type guard

static bool ServiceRegistered(string name) => Com_ServiceOS.Query.IsCreated(name);

Try / catch

try { Com_ServiceOS.Config.Delete(servName); }
catch (Exception ex) when (ex.Message.Contains("无法删除该服务")) { /* already gone; ignore */ }

Prevention

When it happens

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

Common situations: 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.

Related errors


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