OdysseusYuan/LKY_OfficeTools · error · Exception
启动服务 {serv_name} 时失败。未找到该服务!
Error message
启动服务 {serv_name} 时失败。未找到该服务! What it means
Thrown inside Com_ServiceOS.Action.Start when Query.IsCreated(serv_name) returns false (Com_ServiceOS.cs:163-165). IsCreated enumerates ServiceController.GetServices() and matches ServiceName case-insensitively, returning false both when no service matches and when GetServices itself throws. The throw is caught by Start's own catch, logged, and Start returns false (Com_ServiceOS.cs:200-204); callers never see the exception propagate.
Source
Thrown at LKY_OfficeTools/Common/Com_ServiceOS.cs:165
}
catch (Exception Ex)
{
new Log(Ex.ToString());
return false;
}
}
}
internal class Action
{
internal static bool Start(string serv_name)
{
try
{
//未创建服务,不启动!
if (!Query.IsCreated(serv_name))
{
throw new Exception($"启动服务 {serv_name} 时失败。未找到该服务!");
}
//已安装服务,开始启动
using (var control = new ServiceController(serv_name))
{
//没有处于运行状态的服务,才运行。
if (control.Status != ServiceControllerStatus.Running)
{
control.Start();
}
//判断状态,是否是开启了
int wait_time = 0; //等待时间总计
while (wait_time <= (10 * 1000)) //最多等待10秒
{
//获取服务状态
if (Query.RunState(serv_name) == (int)ServiceControllerStatus.Running)
{View on GitHub (pinned to 6f9a1bd471)
Solutions
- Confirm the real service name with `sc query state= all` or by enumerating ServiceController.GetServices() on the target machine.
- Ensure the service is installed before calling Start (e.g. call Config.Create first).
- Check the return value of Start (false means this path or the 10s Running-state wait failed).
- If GetServices is failing, verify the Service Control Manager (services.exe / SCM) is healthy.
Example fix
// before
bool ok = Com_ServiceOS.Action.Start(servName);
// after
if (!Com_ServiceOS.Query.IsCreated(servName)) { /* install/fix name first */ return false; }
bool ok = Com_ServiceOS.Action.Start(servName); Defensive patterns
Strategy: validation
Validate before calling
if (!Com_ServiceOS.Query.IsCreated(servName)) { /* install or fix name first */ return false; }
bool ok = Com_ServiceOS.Action.Start(servName); Type guard
static bool ServiceRegistered(string name) => Com_ServiceOS.Query.IsCreated(name);
Prevention
- Pre-check Query.IsCreated before Start; Start itself returns false rather than throwing to callers.
- Always use the internal ServiceName (not the DisplayName) from `sc query`.
- Ensure the service is installed (Config.Create) in an earlier step before starting it.
- Branch on the bool return: false covers both 'not present' and 'did not reach Running in 10s'.
When it happens
Trigger: Calling Action.Start with a service name that is misspelled, differently cased beyond the lowercased compare, not yet installed, or installed under a different SCM database. Also when GetServices() fails (e.g. SCM service stopped) so IsCreated falls into its catch and returns false.
Common situations: Deplying to a machine where the prerequisite service was never registered; using the DisplayName instead of the internal ServiceName; a fresh OS image before the service installer ran.
Related errors
- 停止服务 {serv_name} 时失败。未找到该服务!
- 重启服务 {serv_name} 时失败。未找到该服务!
- 创建服务 {serv_name} 时,返回值为空!
- 服务 {serv_name} 因无法停止,导致卸载失败!
- 删除服务 {serv_name} 时,返回值为空!
AI-assisted analysis of OdysseusYuan/LKY_OfficeTools@6f9a1bd471 (2026-08-13).
Data as JSON: /api/errors/995d03089be687f6.
Report an issue: GitHub.