Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
375 views
in Technique[技术] by (71.8m points)

c# - Windows service, can't access app.config from within my Installer's constructor

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

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

1 Reply

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...