Reading a collection from the App.config file

I am trying to get a set of items from an application configuration file. Everything looks fine, but I always get 0 items (no matter what I put on the configuration file ...)

My code is:

using System.Configuration; namespace CustomSettingConfiguration { public class TestConfigurationElement : ConfigurationElement { [ConfigurationProperty("name", IsKey = true, IsRequired = true)] public string Name { get { return (string) this["name"]; } } } [ConfigurationCollection(typeof (TestConfigurationElement), AddItemName = "test")] public class TestConfigurationElementCollection : ConfigurationElementCollection { protected override ConfigurationElement CreateNewElement() { return new TestConfigurationElement(); } protected override object GetElementKey(ConfigurationElement element) { return ((TestConfigurationElement) element).Name; } } public class TestConfigurationSection : ConfigurationSection { [ConfigurationProperty("Tests", IsDefaultCollection = true)] public TestConfigurationElementCollection Tests { get { return (TestConfigurationElementCollection)this["Tests"]; } } } } 

And the configuration file:

 <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="TestConfigurationSection" type="CustomSettingConfiguration.TestConfigurationSection" /> </configSections> <TestConfigurationSection> <Tests> <test name="One" /> <test name="Two" /> </Tests> </TestConfigurationSection> </configuration> 

To use it:

  TestConfigurationSection a = new TestConfigurationSection(); var tests = a.Tests; 

Any idea?

Thank you in advance

+4
source share
2 answers

To load configuration parameters, you need to run another code:

 TestConfigurationSection a = (TestConfigurationSection) System.Configuration.ConfigurationManager.GetSection("TestConfigurationSection"); 

also make sure assemply is specified in the configuration file:

 <section name="TestConfigurationSection" type="CustomSettingConfiguration.TestConfigurationSection, ConsoleApplication1" /> 
+3
source

Does he need his own configuration section? Personally, I rarely find it necessary to go beyond the simple settings in the project properties. This is how I did it in a project where I wanted to use a list of sources that were allowed and forbidden. The object that I wanted to save in the configuration (in my case, user.config, but the principle is the same for app.config) is a simple C # object (it implements an interface not associated with this discussion, that's all).

So to make it easier, I created a collection class for my object. This simplifies setup. Here's the class as a whole:

 // This is mainly declared to ease use as a User Setting public class SpellSourceCollection : List<SpellSource> { public SpellSourceCollection() : base() { } public SpellSourceCollection(IEnumerable<SpellSource> ListToCopy) : this() { this.AddRange(ListToCopy); } } 

Remember that SpellSource has nothing special. Now, in the project settings, I can assign Type as my collection object.

setting the property to SpellSourceCollection

You may need to β€œview” the correct user object. However, once this is done, reading from app.config (or user.config) is a breeze. Here's what the configuration file looks like (a bit short).

 <setting name="Sources" serializeAs="Xml"> <value> <ArrayOfSpellSource xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <SpellSource> <Source>PFRPG Advanced Player Guide</Source> <Allowed>true</Allowed> <BackgroundColor>White</BackgroundColor> </SpellSource> <SpellSource> <Source>PFRPG Core</Source> <Allowed>true</Allowed> <BackgroundColor>White</BackgroundColor> </SpellSource> <SpellSource> <Source>Rival Guide</Source> <Allowed>false</Allowed> <BackgroundColor>White</BackgroundColor> </SpellSource> <SpellSource> <Source>Ultimate Combat</Source> <Allowed>true</Allowed> <BackgroundColor>White</BackgroundColor> </SpellSource> <SpellSource> <Source>Ultimate Magic</Source> <Allowed>true</Allowed> <BackgroundColor>Cyan</BackgroundColor> </SpellSource> </ArrayOfSpellSource> </value> </setting> 

Getting a property is just a question.

 SpellSourceCollection settingsSources = Properties.Settings.Default.Sources; // do stuff or even later in your project, you can save this user setting Properties.Settings.Default.Sources = settingsSources; Properties.Settings.Default.Save(); 

You can apply this to your project in a similar way. Only slightly complex bits declare a collection object and create a parameter in the project properties.

+1
source

Source: https://habr.com/ru/post/1388198/


All Articles