7

我有一个在多个实例中在系统中运行的服务。我需要DISPLAY_NAME从服务中找到服务。我已经尝试过ServiceBase.ServiceName,但它(可能)从项目安装程序返回服务的名称,至少在这种情况下是无用的。

该服务是通过installutil一个参数安装的/name=

编辑

我有一个基于 Imran Balouch 答案的解决方法。我在安装程序中读取名称Me.Context.Parameters("name")并将其写入ImagePath注册表子项并在服务中使用 Environment.GetCommandLineArgs 读取它。

4

2 回答 2

6

ProjectInstaller在你的windows服务项目中添加了吗?如果是,则在 ProjectInstaller 中ServiceInstaller为您的服务添加,并且在该 ServiceInstaller 中您可以指定服务的显示名称。选择该 serviceinstaller 并在属性中设置其或Display Name in或者您可以将显示名称指定为:InitializeComponentProjectInstaller.Designer.csProjectInstaller.Designer.vb

this.yourServiceInstaller.DisplayName = "Service Display Name";
于 2012-06-12T12:01:28.697 回答
3

您可以使用以下类获取服务的显示名称,给定它的短 ServiceName ServiceController

ServiceController sc = new ServiceController(this.ServiceName);
var displayName = sc.DisplayName;

正如您所说,您可以ServiceName轻松获得,因为它是您自己的类的成员,继承自ServiceBase. 对于在同一台机器上运行的服务的不同实例,此服务名称将有所不同,因为它是唯一标识符。

于 2012-06-12T11:52:10.577 回答