Windows服务,不能从我安装的构造访问的app.config [英] Windows service, can't access app.config from within my Installer's constructor

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

问题描述

我想我的windows服务的用户名/密码信息存储登录为用户在App.config。

因此​​,在我安装,我想抓住从app.config中的用户名/密码,并设置属性,但我正在试图安装服务时发生错误。

它工作正常,如果c中的用户名/密码,我很难$ C $,当我试图访问在app.config失败

 公共类胡说:安装程序
{

    公共布拉赫()
    {

    ServiceProcessInstaller oServiceProcessInstaller =新ServiceProcessInstaller();
                的ServiceInstaller oServiceInstaller =新的ServiceInstaller();

                oServiceProcessInstaller.Account = ServiceAccount.User;

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

    }
}
 

解决方案

现在的问题是,当你安装程序运行,你仍然在安装阶段,你的应用程序还没有完全安装。在app.config将只提供实际的应用程序运行时。

不过,您可以执行以下操作:

  1. 提示为安装程序中的用户名和密码(或在命令行)。
  2. 将此信息传递给您的安装程序类(google一下)
  3. 在您的安装程序类,有一个变量,它告诉你的安装路径
  4. 在安装程序中相应的事件,使用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:

  1. Prompt the user for the username and password within the installer (or on the command line).
  2. Pass this information to your installer class (google it)
  3. Within your installer class, there is a variable that tells you the installation path
  4. 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屋!

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