How to get windows service name from app.config

假如想象 提交于 2019-11-27 07:51:34

问题


i have a app.config

  <appSettings>
    <add key="ServiceName" value="HasService"/>
    <add key="ServiceDisplayName" value="HasService"/>
  </appSettings>

my service installer class

 [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);
        }
    }

i want to get service name from 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?
    }

how to get service name and service display name from app.config file in MyServiceInstaller class.


回答1:


problem is solved with this code

public string GetServiceNameAppConfig(string serviceName)
{
    var config = ConfigurationManager.OpenExeConfiguration(Assembly.GetAssembly(typeof(MyServiceInstaller)).Location);
    return config.AppSettings.Settings[serviceName].Value;
}



回答2:


have you tried this - configurationmanager.appsettings["yourkey"]



来源:https://stackoverflow.com/questions/8516701/how-to-get-windows-service-name-from-app-config

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!