Reputation: 381
It's a simple windows forms application, one screen and nothing more. I've created my own ConfigurationSection handler
Assembly RemoteFactoryManager.Utils.dll
public class EnvironmentSection : ConfigurationSection
{
public EnvironmentSection() { }
public EnvironmentSection( string description,
string path,
string selected)
{
this.Description = description;
this.Path = path;
this.Selected = selected;
}
[ConfigurationProperty("description", IsRequired = true)]
public string Description
{
get { return this["description"] as string; }
set { this["description"] = value; }
}
[ConfigurationProperty("path", IsRequired = true)]
public string Path
{
get { return this["path"] as string; }
set { this["path"] = value; }
}
[ConfigurationProperty("selected", IsRequired = false)]
public string Selected
{
get { return this["selected"] as string; }
set { this["selected"] = value; }
}
}
This is my app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="Environments">
<section name="DEV" type="RemoteFactoryManager.Utils.EnvironmentSection, RemoteFactoryManager.Utils" />
<section name="DEV1" type="RemoteFactoryManager.Utils.EnvironmentSection, RemoteFactoryManager.Utils" />
<section name="DEV2" type="RemoteFactoryManager.Utils.EnvironmentSection, RemoteFactoryManager.Utils" />
</sectionGroup>
</configSections>
<Enviroments>
<DEV description="Development" path="\\SomeServer\C$\somepath" selected="1" />
<UAT1 description="Devlpmnt - 1" path="\\SomeServer1\C$\somepath" />
<UAT2 description="Devlpmnt - 2" path="\\SomeServer2\C$\somepath" />
</Enviroments>
</configuration>
Still in Assembly RemoteFactoryManager.Utils.dll
public class ConfigReader
{
private List<Env.Environment> _env;
public List<Env.Environment> Environments { get { return _env; } }
public void LoadEnvironments()
{
Configuration oConfiguration = ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None);
ConfigurationSectionGroup oSectionGroup =
oConfiguration.GetSectionGroup("Environments") as
ConfigurationSectionGroup;
if (oSectionGroup != null)
{
_env = new List<Env.Environment>();
foreach (EnvironmentSection oSection in oSectionGroup.Sections)
{
Env.Environment e = new Env.Environment();
e.Description = oSection.Description; // always empty
e.Path = oSection.Path; // always empty
e.Selected = oSection.Selected; // always empty
_env.Add(e);
}
}
}
The .exe code
private void frmMain_Load(object sender, EventArgs e)
{
ConfigReader c = new ConfigReader();
c.LoadEnvironments();
}
My problem is
oSection.Description, oSection.Path and oSection.Selected are always empty.
What am I doing wrong? This used to work fine in another project. The only difference is that now the app.config file belongs to the .exe and the code that handle the sections are in another separated assembly.
Upvotes: 2
Views: 219