Reputation: 4190
I have an App.config with custom configurations
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="wsa" type="WSASRT.SRT.WSA.WsaConfig" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<wsa>
<src E="[email protected]" CN="AnotherFoo" OU="AnotherBar" />
<!-- More elements -->
</wsa>
</configuration>
And the following mapping class
namespace WSASRT.SRT.WSA
{
class WsaConfig : ConfigurationSection
{
[ConfigurationProperty("src")]
public SrcElement Src { get { return (SrcElement)this["src"]; } }
}
public class SrcElement : ConfigurationElement
{
[ConfigurationProperty("E")]
public string E { get { return (String)this["E"]; } }
[ConfigurationProperty("CN")]
public string CN { get { return (String)this["CN"]; } }
}
}
And my Program.cs looks like:
class Program
{
static void Main(string[] args)
{
WsaConfig config = new WsaConfig();
Console.WriteLine(config.Src.CN);
Console.ReadLine();
}
}
When I run it I get an empty string but I should get "AnotherFoo". What am I doing wrong?
Upvotes: 4
Views: 74
Reputation: 6672
Replace the first line as below so that you read it from your config file & not instantiate a new one..
WsaConfig config = ConfigurationManager.GetSection("wsa") as WsaConfig;
Upvotes: 1