2

I have one class:

public class CustomClass
{
    public string Columns;
    public string Filter;

    public string SourceDB;
    public string SourceTable;

    public string DestinationDB;
    public string DestinationTable;
}

In the user settings, I need to store an array of CustomClass. This is because I need the user ability to specifiy multiple CustomClass in the app.config file.

j0k
  • 22,600
  • 28
  • 79
  • 90
Akshay J
  • 5,362
  • 13
  • 68
  • 105
  • Look at implementing your own configuration handler, these can have multiple entries and can be customised to have multiple fields. http://msdn.microsoft.com/en-us/library/2tw134k3(v=vs.100).aspx – ChrisBint Jul 30 '13 at 06:17

2 Answers2

2

You will have to start by creating a setting in your project settings file, let's name it CustomClasses. The next part is a little bit tricky, as it involves editing the XML of the Settings.settings file:

<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" 
              CurrentProfile="(Default)" 
              GeneratedClassNamespace="ConsoleApplication1.Properties" 
              GeneratedClassName="Settings">
  <Profiles />
  <Settings>
    <Setting Name="CustomClasses" 
             GenerateDefaultValueInCode="false" 
             Type="System.Collections.Generic.List&lt;ConsoleApplication1.CustomClass&gt;" 
             Scope="User">
    </Setting>
  </Settings>
</SettingsFile>

If you open your Settings.Designer.cs file, you should have now:

[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public global::System.Collections.Generic
    .List<ConsoleApplication1.CustomClass> CustomClasses {
    get {
        return ((global::System.Collections.Generic
            .List<ConsoleApplication1.CustomClass>)(this["CustomClasses"]));
    }
    set {
        this["CustomClasses"] = value;
    }
}

You can save the setting in your application:

class Program
{
    static void Main(string[] args)
    {
        Properties.Settings.Default.CustomClasses = new List<CustomClass>() {
            new CustomClass(){Columns="columns1"},
            new CustomClass(){Columns="columns2"},
            new CustomClass(){Columns="columns3"},
            new CustomClass(){Columns="columns4"}
        };
        Properties.Settings.Default.Save();
    }
}
Alex Filipovici
  • 31,789
  • 6
  • 54
  • 78
0

You can declare a CustomClassSection in App.config, and declare inside a collection of CustomClass instances. Something like:

<configuration>
    <configSections>
        <section name="CustomClassSection" type = "A type of class section " />
    </configSections>
</configuration>

<CustomClassSection>
    <CustomClass Columns="column1" Filter="filter1" SourceDB="sourcedb1" SourceTable="sourcetable1" DestinationDB="destdb1" DestinationTable="desttable1"/>
    <CustomClass Columns="column2" Filter="filter2" SourceDB="sourcedb2" SourceTable="sourcetable2" DestinationDB="destdb2" DestinationTable="desttable2"/>
...
</CustomClassSection>

You can see how work with sections here: How to create custom config section in app.config?.

Community
  • 1
  • 1
Jose Rodriguez
  • 9,753
  • 13
  • 36
  • 52