Search code examples
c#configurationwindows-servicesservice-accountsservice-installer

Set windows service account in config


I need to set the serviceaccount for a windows service in the config file. But I cannot access the config values in the installer. I read this question but I do not want to use install parameters. Is there any way to do this?

EDIT: And I also tried some solutions in this but I can't access Configuration or ConfigurationManager inside the installer class... Am I supposed to add some reference?


Solution

  • After a lot of research, I found my own solution...

    First add a reference to System.Configuration

    and then add the following code in the installer class

    Assembly service = Assembly.GetAssembly(typeof(ProjectInstaller));
    string assemblyPath = service.Location;
    Configuration config = ConfigurationManager.OpenExeConfiguration(assemblyPath);
    KeyValueConfigurationCollection mySettings = config.AppSettings.Settings;
    processInstaller.Account = (ServiceAccount)Enum.Parse(typeof(ServiceAccount), mySettings["Account"].Value);
    

    I am just so happy right now!! :)