简体   繁体   English

将命令行参数添加到服务安装程序c#

[英]Add commandline arguments to service installer c#

I am working on an application which starts as a service but only if a commandline switch tells it to (otherwise a standard form is opened). 我正在开发一个作为服务启动的应用程序,但只有在命令行开关告诉它时(否则打开标准表单)。 So when the service is started by Windows at bootup, it must pass this commandline option or the service fails to start. 因此,当Windows在启动时启动服务时,它必须通过此命令行选项或服务无法启动。

I would like to have the installer (ServiceProcessInstaller) add a commandline option so that when the service is started it adds the commandline option to the command. 我想让安装程序(ServiceProcessInstaller)添加一个命令行选项,以便在启动服务时将命令行选项添加到命令中。

Example: MyService.exe -commandlineoption 示例:MyService.exe -commandlineoption

I thought this was what the ServiceProcessorInstaller.Context property was for, but that is for the arguments that were executed on InstallUtil. 我认为这就是ServiceProcessorInstaller.Context属性的用途,但这是针对在InstallUtil上执行的参数。

Any suggestions? 有什么建议么?

When I've added command-line options to services, I've always defaulted to running as a service. 当我向服务添加命令行选项时,我一直默认为作为服务运行。 However, I know that the opposite is possible because it's how SvcHost works: it's an EXE that's always configured to load DLL's as services. 但是,我知道相反的情况是可能的,因为它是SvcHost的工作原理:它是一个总是被配置为将DLL作为服务加载的EXE。

A service is only installed once per release. 每个版本只安装一次服务。 It sounds like you are talking about passing a command line argument to the service when it's started. 听起来你正在谈论在服务启动时将命令行参数传递给服务。

You can pass command line arguments to the service when you start it using the ServiceController.Start method : 使用ServiceController.Start方法启动时,可以将命令行参数传递给服务:

using (var controller = new ServiceController("servicename")) {
    controller.Start(arg0, arg1);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM