windows - C# - 服务如何设置启动参数

标签 windows c#-4.0 service

我目前正在开发一个 Windows 服务(启动它是一件好事)。 最大的问题是如何在开始参数字段中获取参数(当然无需手动操作)。

所以我想看到的是以下内容。如果发生以下情况,我希望在安装该服务后。

安装服务并设置启动参数。

怎么会做这样的事情(已经在浏览 StackOverflow 但它不符合我的要求)

enter image description here

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

如果您想了解更多信息,请询问(如果有什么不妥,请不要关闭帖子“只问 :)”)

提前致谢

最佳答案

在您的问题更新后,我了解您要完成的任务。据我目前所知,不使用注册表是无法设置这些启动参数的。您必须从服务控制台或使用安装程序手动执行此操作。当您查看涵盖 ServiceBase.OnStart ( MSDN ServiceBase.OnStart method ) 的 MSDN 页面时,它清楚地指出:

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

问题是,在删除服务时,您仍然需要跟踪这些注册表设置。 因此,此处提供的链接 ("Am I running as a service") 也可能有所帮助。

关于windows - C# - 服务如何设置启动参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27818050/

相关文章:

windows - 是否可以在浏览器中运行 EXE?

asp.net - 将 CheckBoxFor 绑定(bind)到 bool?

Angular - 在兄弟组件之间共享数据而不在实例之间共享

asp.net - 如何在 Asp.net C# 中使用 LDAP 在 Active Directory 中搜索用户

linux - 干净的Redhat上的MongoDB "service"无法启动

delphi - 如何将 (Delphi) OLE 服务器与第二个客户端重用?

java - 使用java注销计算机

windows - 如何将我的 OpenGL 应用程序置于全屏模式?

Windows 7 应用程序问题 - 仅出现在任务栏中

c# - 如何使用 C# 通过 odbc 执行存储过程?