MrProgram
MrProgram

Reputation: 5242

read whole settings section instead of one by one

I have a app.config looking like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings>
    <add key="Setting1" value="value1" />
    <add key="Setting2" value="value2" />
</appSettings>
</configuration>

Is it possible to somehow read the whole section of settings? Instead of just calling them one by one:

private static readonly string Setting1 = ConfigurationManager.AppSettings["Setting1"];
private static readonly int Setting2 = ConfigurationManager.AppSettings["Setting2"];

Is that possible? If not by this way, how should I achieve this (was thinking about creating an own Settings.xml but I wanna try this first).

Upvotes: 0

Views: 19

Answers (1)

Richard
Richard

Reputation: 108975

Just directly use the object returned by ConfigurationManager.AppSettings: it has a whole load of other members.

eg.

var as = ConfigurationManager.AppSettings;
foreach (string k in as.AllKeys) {
  Console.WriteLine("{0}: {1}", k, as[k]);
}

Upvotes: 2

Related Questions