Windows Service Setting user account
I want to set the user account for Windows Service even before installing. I am doing it by adding code in project installer.
this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.User; 开发者_如何学JAVA this.serviceProcessInstaller1.Password = ConfigurationSettings.AppSettings["password"]; this.serviceProcessInstaller1.Username = ConfigurationSettings.AppSettings["username"];
Still its prompting for Username and password. Looks like the configuration file is not getting ready by the time the installation is done.
How I can i pull the username and password from configuration file instead of hardcoding it?
Well, I'm at a loss to say why the AppSettings values are not readable in the traditional manner while the installer is running. I tried it myself and ran into the same problem you are having. However, I was able to get around the problem by loading the configuration file as a regular XML file and reading it that way. Try this:
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;
精彩评论