Setting up a Windows service Setting up a user account

I want to set up a user account for a Windows service even before installation. I do this by adding code to the project installer.

this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.User; this.serviceProcessInstaller1.Password = ConfigurationSettings.AppSettings ["password"]; this.serviceProcessInstaller1.Username = ConfigurationSettings.AppSettings ["username"];

Username and password are still requested. It seems that the configuration file is not being prepared by the time the installation is complete.

How can I pull the username and password from the configuration file, rather than hardcoding?

+4
source share
1 answer

Well, I don’t understand why AppSettings values ​​are not read in the traditional way while the installer is running. I tried it myself and ran into the same problem as yours. However, I was able to work around the problem by downloading the configuration file as a regular XML file and reading it that way. Try the following:

XmlDocument doc = new XmlDocument(); doc.Load(Assembly.GetExecutingAssembly().Location + ".config"); XmlElement appSettings = (XmlElement)doc.DocumentElement.GetElementsByTagName("appSettings")[0]; string username = null; string password = null; foreach (XmlElement setting in appSettings.GetElementsByTagName("add")) { string key = setting.GetAttribute("key"); if (key == "username") username = setting.GetAttribute("value"); if (key == "password") password = setting.GetAttribute("value"); } serviceProcessInstaller1.Account = ServiceAccount.User; serviceProcessInstaller1.Username = username; serviceProcessInstaller1.Password = password; 
+2
source

All Articles