Windows服务,无法从我的安装程序的构造函数中访问app.config [英] Windows service, can't access app.config from within my Installer's constructor
问题描述
因此,在我的安装程序中,我正在尝试使用我的Windows服务的用户名/密码信息。从app.config中获取用户名/密码并设置属性,但是我在尝试安装服务时收到错误。
如果我硬编码用户名/密码,当我尝试访问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();
}
}
问题是,当您的安装程序运行时,您仍处于安装阶段,并且您的应用程序尚未完全安装。
但您可以执行以下操作:
- 在安装程序(或命令行)中提示用户输入用户名和密码。
- 将此信息传递给安装程序类
- 在安装程序类中,有一个变量用于指示安装路径
- 在安装程序的相应事件中,使用System.IO函数以打开app.config文件并插入用户输入的信息
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();
}
}
The problem is that when your installer runs, you are still in installation phase and your application hasn't been fully installed. The app.config will only be available when the actual application is run.
You can however do the following:
- Prompt the user for the username and password within the installer (or on the command line).
- Pass this information to your installer class (google it)
- Within your installer class, there is a variable that tells you the installation path
- Within the appropriate event in the installer, use System.IO functions to open the app.config file and insert the user entered information
这篇关于Windows服务,无法从我的安装程序的构造函数中访问app.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!