我有一个 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 文件获取服务名称和服务显示名称。
最佳答案
问题用这段代码解决了
public string GetServiceNameAppConfig(string serviceName)
{
var config = ConfigurationManager.OpenExeConfiguration(Assembly.GetAssembly(typeof(MyServiceInstaller)).Location);
return config.AppSettings.Settings[serviceName].Value;
}
关于c# - 如何从 app.config 获取 Windows 服务名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8516701/