C# - 服务如何设置启动参数 [英] C# - Services how to set the start parameters

查看:1496
本文介绍了C# - 服务如何设置启动参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个windows服务(这是一个好东西)。
最大的问题是如何在开始参数字段中获取参数(无需手动执行)

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.

如何做这样的事情(已经浏览了StackOverflow,但它不符合我想要的)

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

我问这个问题的原因如下:服务是GUI和接收后端之间的通信层的一部分。如果后端位置不同(例如另一个IP地址),则服务需要相应地具有新地址。

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 :)')

提前感谢

推荐答案

更新您的问题后,我了解您要完成的工作。据我目前所知,不可能不使用注册表设置这些启动参数。您必须从服务控制台或使用安装程序手动进行。当你看看MSDN页面涵盖ServiceBase.OnStart( MSDN ServiceBase.OnStart方法)它清楚地说明:

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:


处理服务的初始化参数OnStart方法,而不是在Main方法中。 args参数数组中的参数可以在服务控制台中的服务的属性窗口中手动设置。在控制台中输入的参数不会保存;当从控制面板启动服务时,它们将一次性传递到服务。服务自动启动时必须显示的参数可以放在服务注册表项(HKEY_LOCAL_MACHINE \SYSTEM \CurrentControlSet\Services \)的ImagePath字符串值中。您可以使用GetCommandLineArgs方法从注册表获取参数,例如:string [] imagePathArgs = Environment.GetCommandLineArgs();

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.

这篇关于C# - 服务如何设置启动参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆