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.

1

2 Answers 2

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();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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?.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.