How to pass parameters in windows service from Installer to Main function in Program.cs?

送分小仙女□ 提交于 2019-12-05 23:00:36

Parameters you send using ServiceController.Start() method are available to you as parameters to the OnStart() method. If I'm not mistaken (It's been a while since I needed to do this).

The OnStart method's signature is:

OnStart(string[] args)

However, if you need the parameters to be sent to the service each time the service is started (automatically) on boot, then you should look at the MSDN documentation on this. Specifically

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();.

http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.onstart.aspx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!