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
- 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.
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
- 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.
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
- 服务 {serv_name} 因无法停止,导致卸载失败!
- 删除服务 {serv_name} 时,返回值为空!
- 删除服务 {serv_name} 失败!
- 尝试修改服务 {serv_name} 的 binPath 信息为 {serv_binpath},但该服务未安装!
- 尝试修改服务 {serv_name} 的 DisplayName 信息为 {serv_displayname},但该服务
AI-assisted analysis of OdysseusYuan/LKY_OfficeTools@6f9a1bd471 (2026-08-13).
Data as JSON: /api/errors/72929cc2c479f338.
Report an issue: GitHub.