C # - Services, how to set initial parameters

I am currently working on a Windows service (which starts in a way that is good). The big question is how can I get the parameters in the launch parameters field (without manual output).

So, I would like to see the following. After installing the service, I would like if the following happens.

Services are installed and startup parameters are set.

How to do it (already browsing StackOverflow, but it doesn't match what I want)

enter image description here

The reason I ask the question is this: this service is part of the communication layer between the GUI and the receiving server. If the backend location is different (for example, a different IP address), the service should have a new address accordingly.

If you want more information, please ask (do not post a message if something is wrong "just ask :)")

Thanks in advance

+4
source share
3 answers

After updating your question, I understand what you are trying to accomplish. As far as I know, it is impossible to set these startup parameters without using the registry. You will need to do this manually from the services console or using the installer. When you look at the MSDN page that includes ServiceBase.OnStart ( the MSDN ServiceBase.OnStart method ), it clearly states:

OnStart, Main. args . , , ; , . , , ImagePath (HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services \). GetCommandLineArgs, : string [] imagePathArgs = Environment.GetCommandLineArgs();

, . , ( " " , .

+3

sc.exe:

c:\>sc config <myservice> binPath="\path\to\myservice.exe -param -param"
+1

In your OnStart()or your service thread, use something like:

string myArg = ConfigurationManager.AppSettings["MyArg"]

where in your App.Config app have you added

<appSettings>
  <!-- My keys -->
  <add key="MyArg" value="xxx"/>
</appSettings>
0
source

Source: https://habr.com/ru/post/1570283/


All Articles