安装了新的 Windows 服务,无法启动:“系统错误 2 ... 系统找不到指定的文件”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19799669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 11:03:36  来源:igfitidea点击:

New Windows Service installed, fails to start: "System error 2 ... system cannot find the file specified"

windowswindows-servicesservicecontroller

提问by Ho Ho Ho

I have installed several other custom .Net windows services successfully. A new one I had recently written was very similar to the others and while it installed without error - on starting it with the service controller it failed to start with the error dialog: System error 2 ... system cannot find the file specified.

我已经成功安装了其他几个自定义的 .Net Windows 服务。我最近写的一个新的与其他的非常相似,虽然它安装没有错误 - 在使用服务控制器启动它时,它无法启动并显示错误对话框:系统错误 2 ... 系统找不到指定的文件。

After time and consternation, the only thing I could think of that was significantly different about this service was that the path and executable name were at least 10 characters longer than any of my other services. On shortening both the path and .exe name and re-installing, the service ran fine: no error! I can only assume my previous path or service or .exe name was too long.

经过时间和惊愕,我能想到的唯一与此服务显着不同的是路径和可执行文件名称至少比我的任何其他服务长 10 个字符。缩短路径和 .exe 名称并重新安装后,服务运行良好:没有错误!我只能假设我以前的路径或服务或 .exe 名称太长。

Also, It would be pertinent to mention I had used some borrowed "service driver" code built in to my exe to handle the install/uninstall of the service to the service controller via win API calls. It could be a character limit was hidden within that service driver module.

此外,值得一提的是,我使用了一些内置于我的 exe 中的借用“服务驱动程序”代码来处理通过 win API 调用将服务安装/卸载到服务控制器。可能是该服务驱动程序模块中隐藏了字符限制。

I could not find any windows related docs to confirm if there is a system bound character limit to a path or service name that I had exceeded. I will dig in to the service driver when time permits and see if that turns out to be the problem. Meanwhile I welcome any insights.

我找不到任何与 Windows 相关的文档来确认我是否超出了路径或服务名称的系统绑定字符限制。如果时间允许,我将深入了解服务驱动程序,看看是否是问题所在。同时,我欢迎任何见解。

回答by Ho Ho Ho

I experimented with some test services and found it was not the length of any property that caused my problem (“System error 2 ... system cannot find the file specified”) to begin with. My built in service installer uses three properties: ServiceName, ServiceTitle, ServiceDescription. On installing, I found that it writes full service path to the registry, but it doesn't just take the actual exe (assembly) name, it uses the ServiceName property to build the path! My issue was that the ServiceName and assembly name didn't match, hence file not found. I used a powershell registry query to expose the path and finally noticed the mismatch from there. When I first noticed the problem I had not noticed that when I shortened the service name from whatever it was – that I just used the assembly name without the .exe and that is what actually fixed it, not simply shortening it.

我尝试了一些测试服务,发现不是任何属性的长度导致了我的问题(“系统错误 2 ... 系统找不到指定的文件”)。我的内置服务安装程序使用三个属性:ServiceName、ServiceTitle、ServiceDescription。在安装时,我发现它将完整的服务路径写入注册表,但它不只是采用实际的 exe(程序集)名称,它使用 ServiceName 属性来构建路径!我的问题是 ServiceName 和程序集名称不匹配,因此找不到文件。我使用了一个 powershell 注册表查询来公开路径,最后注意到那里的不匹配。当我第一次注意到这个问题时,我没有注意到当我缩短服务名称时——我只是使用了没有 .

回答by jasttim

I had a similar issue with a service, where I was getting the same error.

我在服务上遇到了类似的问题,在那里我遇到了同样的错误。

I went to:

我去了:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\YourServiceName\ImagePath

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\YourServiceName\ImagePath

My 'ImagePath' was set to a virtual drive called "W:\" that exists on "C:\".
I replaced this path with the actual file location on the C:\ drive and then the service started successfully

我的“ImagePath”被设置为一个名为“W:\”的虚拟驱动器,它存在于“C:\”上。
我将此路径替换为 C:\ 驱动器上的实际文件位置,然后服务成功启动

回答by Xpleria

In my case, I opened the Command Promt and navigated to the exe and installed it from there. So I did not enter the full path. Once I used the full path, it worked.

就我而言,我打开命令提示符并导航到 exe 并从那里安装它。所以我没有输入完整路径。一旦我使用了完整路径,它就起作用了。

  1. So, you need to either install the service with the full path or add the exe file's path to the PATH in system environment variables.

    SC CREATE "Service-Name" binpath="D:\full-path-to-service\service.exe"
    

    or add D:\full-path-to-service\to PATH variable and use

    SC CREATE "Service-Name" binpath="service.exe"
    
  1. 因此,您需要使用完整路径安装服务或将 exe 文件的路径添加到系统环境变量中的 PATH 中。

    SC CREATE "Service-Name" binpath="D:\full-path-to-service\service.exe"
    

    或添加D:\full-path-to-service\到 PATH 变量并使用

    SC CREATE "Service-Name" binpath="service.exe"
    



  1. Also, verify that the path is correct. Because, I once installed with the wrong path and it got successfully installed, but when I tried to start it, I got the same error.
  1. 此外,请验证路径是否正确。因为,我曾经用错误的路径安装并成功安装,但是当我尝试启动它时,我遇到了同样的错误。

回答by AgathoSAreS

My Problem was, creating the Service with Powershell command added brakets like: <C:\Path\To\Service\Service.exe>to the registry.

我的问题是,使用 Powershell 命令创建服务<C:\Path\To\Service\Service.exe>会向注册表添加类似以下内容的刹车。

Replacing < and > with " fixed it for me.

回答by Zac

I had same issue, nothing did solve this error, then I resolved by notusing the c:\Windows\System32path to store the service executable!

我有同样的问题,没有解决这个错误,然后我通过使用c:\Windows\System32路径来存储服务可执行文件来解决!

回答by Олександр Музичук

In my case, the problem was caused by a mistake in the service start routine. DriverEntry (in my case it was a kernel-mode driver) returns a bad status value. I think this situation applies to user mode too.

就我而言,问题是由服务启动例程中的错误引起的。DriverEntry(在我的例子中它是一个内核模式驱动程序)返回一个错误的状态值。我认为这种情况也适用于用户模式。