sin*_*ici 5 c# windows-services
我有一个app.config
<appSettings>
<add key="ServiceName" value="HasService"/>
<add key="ServiceDisplayName" value="HasService"/>
</appSettings>
Run Code Online (Sandbox Code Playgroud)
我的服务安装程序类
[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);
}
}
Run Code Online (Sandbox Code Playgroud)
我想从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?
}
Run Code Online (Sandbox Code Playgroud)
如何从MyServiceInstaller类中的app.config文件获取服务名称和服务显示名称.
sin*_*ici 20
问题通过此代码解决
public string GetServiceNameAppConfig(string serviceName)
{
var config = ConfigurationManager.OpenExeConfiguration(Assembly.GetAssembly(typeof(MyServiceInstaller)).Location);
return config.AppSettings.Settings[serviceName].Value;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
12215 次 |
最近记录: |