{"record":{"id":"995d03089be687f6","repo":"OdysseusYuan/LKY_OfficeTools","slug":"serv-name","errorCode":null,"errorMessage":"启动服务 {serv_name} 时失败。未找到该服务！","messagePattern":"启动服务 (.+?) 时失败。未找到该服务！","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"LKY_OfficeTools/Common/Com_ServiceOS.cs","lineNumber":165,"sourceCode":"                }\n                catch (Exception Ex)\n                {\n                    new Log(Ex.ToString());\n                    return false;\n                }\n            }\n        }\n\n        internal class Action\n        {\n            internal static bool Start(string serv_name)\n            {\n                try\n                {\n                    //未创建服务，不启动！\n                    if (!Query.IsCreated(serv_name))\n                    {\n                        throw new Exception($\"启动服务 {serv_name} 时失败。未找到该服务！\");\n                    }\n\n                    //已安装服务，开始启动\n                    using (var control = new ServiceController(serv_name))\n                    {\n                        //没有处于运行状态的服务，才运行。\n                        if (control.Status != ServiceControllerStatus.Running)\n                        {\n                            control.Start();\n                        }\n\n                        //判断状态，是否是开启了\n                        int wait_time = 0;                          //等待时间总计\n                        while (wait_time <= (10 * 1000))            //最多等待10秒\n                        {\n                            //获取服务状态\n                            if (Query.RunState(serv_name) == (int)ServiceControllerStatus.Running)\n                            {","sourceCodeStart":147,"sourceCodeEnd":183,"githubUrl":"https://github.com/OdysseusYuan/LKY_OfficeTools/blob/6f9a1bd471918f3310e6be4a6d7f42d0d6e467cf/LKY_OfficeTools/Common/Com_ServiceOS.cs#L147-L183","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nbool ok = Com_ServiceOS.Action.Start(servName);\n\n// after\nif (!Com_ServiceOS.Query.IsCreated(servName)) { /* install/fix name first */ return false; }\nbool ok = Com_ServiceOS.Action.Start(servName);","handlingStrategy":"validation","validationCode":"if (!Com_ServiceOS.Query.IsCreated(servName)) { /* install or fix name first */ return false; }\nbool ok = Com_ServiceOS.Action.Start(servName);","typeGuard":"static bool ServiceRegistered(string name) => Com_ServiceOS.Query.IsCreated(name);","tryCatchPattern":null,"preventionTips":["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'."],"tags":["windows-service","scm","service-start"],"backgroundTag":null,"analyzedSha":"6f9a1bd471918f3310e6be4a6d7f42d0d6e467cf","analyzedAt":"2026-08-13T18:00:34.810Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}