Windows服务-在“可执行文件的路径"中提供参数 [英] Windows service - supplying arguments in "path to executable"

查看:63
本文介绍了Windows服务-在“可执行文件的路径"中提供参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

启动Windows服务时,我无法弄清楚如何将参数传递给我的Windows服务.我正在使用标准的.NET类(例如ServiceBase)来实现(并通过ServiceProcessInstaller和ServiceInstaller来安装)我的服务.

I cannot figure out how to pass (constant) arguments into my Windows service when it is started. I'm using the standard .NET classes like ServiceBase to implement (and ServiceProcessInstaller and ServiceInstaller to install) my service.

在Windows服务属性对话框(一旦安装)的常规选项卡上,有一个可执行文件路径",在其中我可以看到某些标准Windows服务已指定了命令行参数. System.ServiceProcess.ServiceBase.OnStart 使用 string [] args ,我认为这将允许从.NET代码中访问这些参数.

On the general tab of a Windows Service properties dialog box (once installed), there's a "Path to executable" in which I can see that some of the standard Windows services have command line arguments specified. System.ServiceProcess.ServiceBase.OnStart takes string[] args, which I presume would enable these arguments to be accessed from within .NET code.

ServiceProcessInstaller或ServiceInstaller上是否有一些属性可以设置为允许我将启动参数传递给我自己的服务,还是有人知道应该怎么做?

Are there some properties on ServiceProcessInstaller or ServiceInstaller that I can set to allow me to pass startup arguments to my own service, or does anyone know how it's supposed to be done?

推荐答案

当用户从命令行使用sc.exe start命令手动启动服务时,将提供OnStart()参数.或者可以通过ServiceControl.Start(string [])方法重载以编程方式完成.这很少有用,您可能希望服务自动启动而不需要用户登录.

The OnStart() arguments are supplied when the user starts the service by hand using the sc.exe start command from the command line. Or it can be done programmatically with the ServiceControl.Start(string[]) method overload. This is rarely useful, you probably want your service to start automatically without the requiring the user to logon.

是的,ImagePath注册表项确实支持将参数传递给.exe,您将通过Main(string [])入口点获取它们.不幸的是,ServiceInstaller不支持此功能.更好的方法是使用注册表.在安装程序中,创建HKLM \ System \ CurrentControlSet \ services \ yourServiceName \ Parameters项并向其中写入值.并通过服务的Main或OnStart方法将它们读回.

Yes, the ImagePath registry key does support passing arguments to the .exe, you'll get them through the Main(string[]) entrypoint. Unfortunately, ServiceInstaller doesn't support this. A better way would be to use the registry. In your installer, create the HKLM\System\CurrentControlSet\services\yourServiceName\Parameters key and write values to it. And read them back in your service's Main or OnStart method.

这篇关于Windows服务-在“可执行文件的路径"中提供参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆