I want to read some values from my project's App.config file, but when I do this:
var appsettings = System.Configuration.ConfigurationManager.AppSettings;
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.
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.
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!
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.
Here is the documentation for AppSettings which shows an example http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With