无法从 App.config 读取密钥 [英] Can't read keys from App.config

查看:29
本文介绍了无法从 App.config 读取密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的项目的 App.config 文件中读取一些值,但是当我这样做时:

I want to read some values from my project's App.config file, but when I do this:

var appsettings = System.Configuration.ConfigurationManager.AppSettings;

我只得到四个条目:StopTestRunCallTimeoutInSecondsLogSizeLimitInMegsCreateTraceListenerGetCollectorDataTimeout,它们甚至不是列在 XML 中.我的密钥 usernamepassword 甚至没有出现.

I only get four entries: StopTestRunCallTimeoutInSeconds, LogSizeLimitInMegs, CreateTraceListener and GetCollectorDataTimeout, which aren't even listed in the XML. My keys username and password do not even appear.

我的 App.Config 如下所示:

My App.Config looks like this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <appSettings>
    <add key="username" value="Administrator" />
    <add key="password" value="password" />
  </appSettings>
  <system.web>
    <membership defaultProvider="ClientAuthenticationMembershipProvider">
      <providers>
        <add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
      </providers>
    </membership>
    <roleManager defaultProvider="ClientRoleProvider" enabled="true">
      <providers>
        <add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
      </providers>
    </roleManager>
  </system.web>
</configuration>

我错过了什么吗?在我看来,我正在读取一个完全不同的配置文件,但没有其他配置文件.

Am I missing something? It seems to me like I was reading from a totally different config file, but there is no other one.

推荐答案

我找到了解决方案!问题是,我调试了一个单元测试,它位于同一解决方案的不同子项目中.我将 app.config 文件复制到与单元测试相同的项目中,现在它工作正常.感谢您的帮助!

I found the solution! The problem was, that I debugged a unit test, which was located in a different sub-project within the same solution. I copied the app.config file to the same project as the unit test and it works fine now. thanks for your help!

这篇关于无法从 App.config 读取密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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