Windows 服务,无法从我的安装程序的构造函数中访问 app.config [英] Windows service, can't access app.config from within my Installer's constructor

查看:37
本文介绍了Windows 服务,无法从我的安装程序的构造函数中访问 app.config的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

因此,在我的安装程序中,我试图从 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.

如果我对用户名/密码进行硬编码,它可以正常工作,但在我尝试访问 app.config 时失败

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();

    }
}

推荐答案

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

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:

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

这篇关于Windows 服务,无法从我的安装程序的构造函数中访问 app.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆