Search code examples
c#application-settings

Use multiple setting files and select them at run time


As of today, I have an App.config file in my project.

I use my config settings in the program like this:

ConfigurationManager.AppSettings["FileDir"];

Where FileDir is a setting in App.config defined like this:

<appSettings>
  <add key="FileDir" value="C:\MyFolder" />
</appSettings>

In my build folder (be it Debug or Release) the file will show up as MyProject.exe.config if my project is named MyProject.

I'd like to add multiple similar config files with the exact same variables but different values and I'd like to point to the config file using command line arguments so that I can reuse the same program but with different settings.

How can I tell my program which config file to choose?


Solution

  • I would do something like this:

    <appSettings>
        <add key="WordV1" value="abc" />
        <add key="WordV2" value="def" />
        <add key="WordV3" value="ghi" />
    </appSettings>
    

    string word = ConfigurationManager.AppSettings["WordV" + user.version];