Can I specify a service name in the app.config file?

I want to specify the name of my service in app.config, without requiring reinstalling and installing / removing my service. But just getting the service name from app.config, the service seems to ignore it. Are there any special tricks how to get this? Thanks in advance.

I mean the classic Windows service. I don't think any code is needed here. I just want to get the service name from app.config dynamically.

+7
installer app-config windows-services
source share
5 answers

After a long search on the Internet and reading articles, it became clear to me that the service name cannot be specified in app.config so dynamically, instead, the sc command can be used to perform a similar solution. You can specify other configuration variables in app.config and use sc to rename it.

sc.exe create "servicename" binPath = "myservicepath.exe"

+4
source share

I'm not sure which scenario you have in mind. You want to change the name of your Windows service. Fair. When will this change?

Imagine that you have found a solution and created such a Windows service. I assume that in your scenario you should install it at least for the first time. Then you do not want to uninstall / install it. But presumably you would like to start / stop and do other things with it. Will one of these actions change the name of the service?

If so, I think you can start a process that uninstalls and installs it with a different name for you transparently, based on some kind of naming logic.

I don’t understand how to do this.

Or just come up with a really common name to cover all the possibilities (which can be incredibly simple or incredibly difficult).

+3
source share

http://www.codeproject.com/Articles/21320/Multiple-Instance-NET-Windows-Service

<add key="ServiceName" value="I"/> [RunInstaller(true)] public class ServiceInstaller1 : Installer { internal static string ServiceNameDefault = "My Service"; internal static string ServiceName = GetConfigurationValue("ServiceName"); /// <summary> /// Public Constructor for WindowsServiceInstaller. /// - Put all of your Initialization code here. /// </summary> public ServiceInstaller1() { var serviceProcessInstaller = new ServiceProcessInstaller(); var serviceInstaller = new ServiceInstaller(); //# Service Account Information serviceProcessInstaller.Account = ServiceAccount.LocalSystem; //serviceProcessInstaller.Username = null; //serviceProcessInstaller.Password = null; //# Service Information serviceInstaller.DisplayName = ServiceName; serviceInstaller.StartType = ServiceStartMode.Manual; //# This must be identical to the WindowsService.ServiceBase name //# set in the constructor of WindowsService.cs serviceInstaller.ServiceName = ServiceName; Installers.Add(serviceProcessInstaller); Installers.Add(serviceInstaller); } private static string GetConfigurationValue(string key) { Assembly service = Assembly.GetAssembly(typeof(Service)); Configuration config = ConfigurationManager.OpenExeConfiguration(service.Location); if (config.AppSettings.Settings[key] != null) return ServiceNameDefault + " " + config.AppSettings.Settings[key].Value; else return ServiceNameDefault; } } 
+3
source share

Assuming you mean Windows Service, the answer is no. The service must be installed in the registry, and this name is one of the registry keys.

+2
source share

I am afraid that what you are trying to do is not possible. In fact, this contradicts the nature of the purpose of the Windows service and the current behavior.

After installing the Windows service, the name cannot be changed without reinstalling. In fact, service names are an element named service installer . Which by now, I assume that you know what it is and where it is.

However, there are ways to manipulate the installed service using the Windows Management Instrumentation (WMI). Perhaps this, combined with Isabela's recommendation, will be the right way to your decision.

I would recommend you read the following tutorial, you can find an alternative way to achieve what you are trying to do.

http://www.serverwatch.com/tutorials/article.php/1576131/Windows-Services-Management-With-WMI-Part-1.htm

+2
source share

All Articles