Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get windows service name from app.config

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.

like image 751
sinanakyazici Avatar asked Dec 15 '11 07:12

sinanakyazici


People also ask

How to run a Windows Service in Visual Studio?

Open Visual Studio and from the menus select "File" -> "New" -> "Project...". A New Project window will open. Choose "Visual C#" >> "Windows" project type and select "Windows Service" from the right hand side and name the project "TestWindowsService" as shown in the following screenshot.

How to Create installer Windows Service?

With the designer in focus, right-click, and then click Add Installer. A new class, ProjectInstaller , and two installation components, ServiceProcessInstaller and ServiceInstaller, are added to your project, and property values for the service are copied to the components.


1 Answers

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;
}
like image 93
sinanakyazici Avatar answered Oct 13 '22 16:10

sinanakyazici