Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

app-config

I am in the process of writing a service using C# and I need to store a list of strings within the app.config file. I have 161 of these.

How can I store this information in the app.config file? There must be a way, because these are strongly typed values and thus I'm supposed to easily use any valid .NET type in the code to access them!

I want to avoid having one value that uses a comma-separated list for obvious performance issues.

like image 474
qwe Avatar asked Oct 24 '11 07:10

qwe


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

I use Microsoft’s Visual Studio 2010.

  1. In Solution Explorer, expand the Properties node of your project.

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

  3. In Settings Designer, set the Name, Type, Scope, and Value for your setting. Each row represents a single setting.

  4. The Type that you need is System.Collections.Specialized.StringCollection. This can be located after clicking Browse at the end of the DropDownList that appears when you click to set the Type.

  5. Click on the button that appears towards the end of the Value TextBox.

  6. Type in your strings, one-by-one, in the dialog that appears.

This is how it goes.

like image 115
OmarOthman Avatar answered Oct 13 '22 20:10

OmarOthman


There is a good article about storing lists (or any custom object) in your app.config files in Best Way Of Saving Lists in App.Config

Essentially, you create an object that represents the data.

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

And write a config 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 in your app.config file...

Tell app.config about your cool config section

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

And then add your config 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>
like image 24
Fenton Avatar answered Oct 13 '22 19:10

Fenton