Windows服务无法从我的Installer构造函数中访问app.config

Bla*_*man 8 .net c# security configuration windows-services

我想将我的Windows服务'登录的用户名/密码信息存储为app.config中的'用户.

所以在我的安装程序中,我试图从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();

    }
}
Run Code Online (Sandbox Code Playgroud)

小智 13

关于访问安装程序中的配置文件的一些想法.

Configuration config = ConfigurationManager.OpenExeConfiguration(assemblyPath);
ConnectionStringsSection csSection = config.ConnectionStrings;
Run Code Online (Sandbox Code Playgroud)

Assembly Path可以通过以下几种方式获得:内部Installer类实现:

this.Context.Parameters["assemblypath"].ToString();
Run Code Online (Sandbox Code Playgroud)

或有时反思:

Assembly service = Assembly.GetAssembly(typeof(MyInstaller));
string assemblyPath = service.Location;
Run Code Online (Sandbox Code Playgroud)


Rob*_*ner 6

问题是,当您的安装程序运行时,您仍处于安装阶段,并且您的应用程序尚未完全安装.app.config仅在运行实际应用程序时可用.

但是,您可以执行以下操作:

  1. 在安装程序(或命令行)中提示用户输入用户名和密码.
  2. 将此信息传递给您的安装程序类(谷歌)
  3. 在安装程序类中,有一个变量可以告诉您安装路径
  4. 在安装程序中的相应事件中,使用System.IO函数打开app.config文件并插入用户输入的信息