Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Services how to set the start parameters

I am currently working on a windows service (which starts so that is a good thing). The big question is how can I get parameters in the start parameter field (without doing it manual ofcourse).

So what I would like to see is the following. Upon installation of the service I would like it if the following happens.

The services gets installed and the start parameters are set.

How would one do such a thing (already been browsing StackOverflow but it doesn't comply with what I want)

enter image description here

The reason I ask the question is the following: The service is part of a communication layer between GUI and a receiving backend. If the backend location differs (e.g. another IP address) the service needs to have the new address accordingly.

If you would like to have some more info please ask (don't down the post if something is not in order 'just ask :)')

Thanks in advance

like image 282
Blaatz0r Avatar asked Jan 07 '15 11:01

Blaatz0r


1 Answers

After the update of your question, I understand what you are trying to accomplish. As far as I currently know, it is not possible to set these start parameters without using the registry. You'll have to do it manually from the services console or by using an installer. When you look at the MSDN page covering ServiceBase.OnStart (MSDN ServiceBase.OnStart method) it clearly states:

Process initialization arguments for the service in the OnStart method, not in the Main method. The arguments in the args parameter array can be set manually in the properties window for the service in the Services console. The arguments entered in the console are not saved; they are passed to the service on a one-time basis when the service is started from the control panel. Arguments that must be present when the service is automatically started can be placed in the ImagePath string value for the service's registry key (HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\). You can obtain the arguments from the registry using the GetCommandLineArgs method, for example: string[] imagePathArgs = Environment.GetCommandLineArgs();

Thing is that you will still have to keep track of these registry settings when removing the service. Therefore the link provided here ("Am I running as a service") might help out as well.

like image 171
RvdV79 Avatar answered Sep 22 '22 00:09

RvdV79