the item property of the ConfigurationSectionCollection doesn't work

T

Tony Johansson

Hi!

The initial configuration file is at the bottom.
This program is adding the custom section LabSection to the configuration
file.

My question is based on this text
"You can use an object of the ConfigurationSectionCollection class to read
all the configuration sections in a configuration file. The
ConfigurationSectionCollection class consists of the objects of the
ConfigurationSection class. The ConfigurationSectionCollection class items
can be accessed through the collection iterator. You can use the item
property of the ConfigurationSectionCollection class to access the objects
of the ConfigurationSection class."

The last sentence in this text it says.You can use the item property of the
ConfigurationSectionCollection class to access the objects of the
ConfigurationSection class.
I get the ConfigurationSectionCollection like this.
ConfigurationSectionCollection csc = config.Sections;
So now I have a ConfigurationSectionCollection containing a LabSection how
do I use item property as they say that I can do in the text ?


class Program
{
private static void WriteSettings()
{
try
{
LabSection labSec;
Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

if (config.Sections["LabSection"] == null)
{
labSec = new LabSection();
config.Sections.Add("LabSection", labSec);
labSec.SectionInformation.ForceSave = true;
labSec.FirstName = "Karl";
labSec.LastName = "Otto";
config.Save(ConfigurationSaveMode.Full);

ConfigurationSectionCollection csc = config.Sections;

foreach(string s in config.Sections.Keys)
{
Console.WriteLine(s);
}
}
}
catch (ApplicationException ex)
{
Console.WriteLine(ex.ToString());
}
}

static void Main(string[] args)
{
WriteSettings();
}
}

public class LabSection : ConfigurationSection
{
[ConfigurationProperty("LastName", IsRequired = false, DefaultValue =
"NotGiven")]
public string LastName
{
get { return (string)base["LastName"]; }
set { base["LastName"] = value; }
}

[ConfigurationProperty("FirstName", IsRequired = false, DefaultValue =
"NotGiven")]
public string FirstName
{
get { return (string)base["FirstName"]; }
set { base["FirstName"] = value; }
}
}

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Foo" value="Hello World"/>
</appSettings>
</configuration>

//Tony
 
W

Willem van Rumpt

Tony said:
The last sentence in this text it says.You can use the item property of the
ConfigurationSectionCollection class to access the objects of the
ConfigurationSection class.
I get the ConfigurationSectionCollection like this.
ConfigurationSectionCollection csc = config.Sections;
So now I have a ConfigurationSectionCollection containing a LabSection how
do I use item property as they say that I can do in the text ?

Your confusion probably stems from the fact that the Item property of
the ConfigurationSectionCollection is an indexed property. "Under the
hood", a property called Item is generated for this. As such, it won't
show up in code completion. The way to access it, is by indexing it. In
your code, you can access it with

ConfigurationSection = config.Sections["LabSection"];

See http://msdn.microsoft.com/en-us/library/w7xfeyax(VS.80).aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top