C # application configuration "app.config file" ... how to store collections?

I am writing a service using C # and I need to save the list of lines in the app.config file. I have 161 of them.

How to save this information in app.config file? There must be a way, because these are strongly typed values, and therefore I should easily use any valid .NET type in the code to access them!

I want to avoid using a single value that uses a comma separated list for obvious performance issues.

+7
source share
3 answers

I am using Microsoft Visual Studio 2010.

  • In Solution Explorer, expand the node properties of your project.

  • In Solution Explorer, double-click the .settings file to which you want to add a new parameter. The default name for this file is Settings.settings.

  • In the parameter constructor, set Name , Type , Scope and Value for your setting. Each line represents a single setting.

  • The type you need is System.Collections.Specialized.StringCollection . This can be found after clicking Browse at the end of the DropDownList, which appears when you click to set the Type .

  • Click the button that appears at the end of the Value text box.

  • In the dialog that appears, enter your lines, one at a time.

This is how it goes.

+11
source

There is a good article about storing lists (or any custom object) in app.config files in the Best way to save lists in App.Config

Essentially, you are creating an object that represents data.

 public class MyConfig { public string[] myList; public string someOtherValueIfYouWant; } 

And write a configuration handler for it ...

 public class ConfigSectionHandler : IConfigurationSectionHandler { public const string SECTION_NAME = "MyConfig"; public object Create(object parent, object configContext, XmlNode section) { string szConfig = section.SelectSingleNode("//MyConfig").OuterXml; MyConfig retConf = null; if (szConfig != string.Empty || szConfig != null) { XmlSerializer xsw = new XmlSerializer(typeof(MyConfig)); MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(szConfig)); ms.Position = 0; retConf = (MyConfig)xsw.DeSerialize(ms); } return retConf; } } 

And this allows you to put the following XML file in the app.config file ...

Tell app.config about your cool configuration section.

 <configSections> <section name="MyConfig" type="ConfigSectionHandler,someAssembly" /> </configSection> 

And then add the configuration section ...

 <MyConfig> <myList>First one</myList> <myList>Second one</myList> <myList>Keep going</myList> <myList>And so on</myList> <someOtherValueIfYouWant>some non array config</someOtherValueIfYouWant> </MyConfig> 
+10
source

stack overflow

StringCollection is NOT recommended in VS2015RC for the purpose of annoying when loading the properties dialog for the project and re-saving the data. If you have a dozen entries for serializeAs = "Xml", and each time you open the properties window, it pops up a dialog box for writing EACH, which tells you to confirm the overwrite. Yes / No absolutely nothing.

Data mismatch for each file will be disabled and will become a known issue. See my other post.

I would ask you to use VS2010 and offer it as such, but I do not have enough reputation, and it persists through VS2015RC.

Use a workaround, such as Custom Config storage, or find a way to leave your collections outside the constructor or app.config. One or the other.

A viable solution only for those who wish to put up with the nag dialog boxes or rarely edit settings properties information.

Would you recommend screens to manufacture or launch any commercial project ?: P

-3
source

All Articles