Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to store the username/password information of my windows service 'logon as' user in the app.config.

So in my Installer, I am trying to grab the username/password from app.config and set the property but I am getting an error when trying to install the service.

It works fine if I hard code the username/password, and fails when I try and access the app.config

public class Blah : Installer
{

    public Blah()
    {

        ServiceProcessInstaller oServiceProcessInstaller = new ServiceProcessInstaller();
                ServiceInstaller oServiceInstaller = new ServiceInstaller();            

                oServiceProcessInstaller.Account = ServiceAccount.User;

        oServiceProcessInstaller.Username =             ConfigurationManager.AppSettings["ServiceProcessUsername"].ToString();

    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
366 views
Welcome To Ask or Share your Answers For Others

1 Answer

Just some ideas on accessing config files inside an installer.

Configuration config = ConfigurationManager.OpenExeConfiguration(assemblyPath);
ConnectionStringsSection csSection = config.ConnectionStrings;

Assembly Path can be gotten several ways: Inside Installer class implementation with:

this.Context.Parameters["assemblypath"].ToString();

or sometimes with reflection:

Assembly service = Assembly.GetAssembly(typeof(MyInstaller));
string assemblyPath = service.Location;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...