views:

28

answers:

1

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.

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.

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?

+1  A: 

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.

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.

Hans Passant
Thanks. The Registry view from within the setup projects is a little clunky for my liking. I tried using a custom class derived from ServiceInstaller where I overrode the Install method, but the base class put quotes around the entire path and invalidated what I was trying to achieve. I guess that if I really need to do this, it will require some thought... but I will probably find another way of passing values into the service.
Jono