【发布时间】:2011-12-15 07:46:18
【问题描述】:
我有一个 app.config
<appSettings>
<add key="ServiceName" value="HasService"/>
<add key="ServiceDisplayName" value="HasService"/>
</appSettings>
我的服务安装程序类
[RunInstaller(true)]
public class MyServiceInstaller : System.Configuration.Install.Installer
{
public MyServiceInstaller()
{
var process = new ServiceProcessInstaller {Account = ServiceAccount.LocalSystem};
var serviceAdmin = new ServiceInstaller
{
StartType = ServiceStartMode.Manual,
ServiceName = "HasService",
DisplayName = "HasService"
};
Installers.Add(process);
Installers.Add(serviceAdmin);
}
}
我想从 app.config 中获取服务名称。
var serviceAdmin = new ServiceInstaller
{
StartType = ServiceStartMode.Manual,
ServiceName = GetServiceNameAppConfig("ServiceName"),
DisplayName = GetServiceNameAppConfig("ServiceDisplayName")
};
public string GetServiceNameAppConfig(string serviceName)
{
//what should i write here?
}
如何从 MyServiceInstaller 类的 app.config 文件中获取服务名称和服务显示名称。
【问题讨论】:
-
您为什么要这样做?我可以看到将“有一天可能会改变的东西”放入配置文件的论点,但是您是否会认真地将服务的名称放在此类别中?您也可以根据需要更改配置文件,但除非您重新安装服务(即除非 MsServiceInstaller 完成它的工作),否则您实际上不会更改服务名称。对于必须维护的人来说,这可能会让人感到困惑。
-
@Pete 我想用两个不同的名称运行服务。
-
@sinanakyazici 它对我有用!非常感谢
标签: c# windows-services