C# 无法从 App.config 读取密钥
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16196695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't read keys from App.config
提问by wodzu
I want to read some values from my project's App.config file, but when I do this:
我想从我的项目的 App.config 文件中读取一些值,但是当我这样做时:
var appsettings = System.Configuration.ConfigurationManager.AppSettings;
I only get four entries: StopTestRunCallTimeoutInSeconds, LogSizeLimitInMegs, CreateTraceListenerand GetCollectorDataTimeout, which aren't even listed in the XML. My keys usernameand passworddo not even appear.
我只得到四项:StopTestRunCallTimeoutInSeconds,LogSizeLimitInMegs,CreateTraceListener和GetCollectorDataTimeout,其中甚至没有在XML列。我的钥匙username,password甚至不出现。
My App.Config looks like this:
我的 App.Config 看起来像这样:
<?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.
我错过了什么吗?在我看来,我正在阅读一个完全不同的配置文件,但没有其他配置文件。
采纳答案by wodzu
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 文件复制到与单元测试相同的项目中,现在它工作正常。感谢您的帮助!
回答by Sean
you can read your application settings by pulling them by name
您可以通过按名称拉来读取您的应用程序设置
var username = ConfigurationManager.AppSettings["username"]
var password = ConfigurationManager.AppSettings["password"]
The underlying data type of AppSettings is NameValueCollection. So you pull the value based on the key.
AppSettings 的基础数据类型是 NameValueCollection。所以你根据键拉取值。
Here is the documentation for AppSettings which shows an example http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx
这是 AppSettings 的文档,其中显示了一个示例 http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx
回答by もしもし
It sounds like you're reading from the QTAgentService.exe.config instead of your app.config.
听起来您正在读取 QTAgentService.exe.config 而不是 app.config。

