Click here to Skip to main content
13,516,413 members
Click here to Skip to main content
Add your own
alternative version

Stats

135.8K views
48 bookmarked
Posted 14 Jun 2006

Specify a Configuration File at Runtime for a C# Console Application

, 14 Jun 2006
Rate this:
Please Sign up or sign in to vote.
Using an app.config file with a console application is a great way to manage settings for a console app. However there is no method to specify a config file at runtime. Here is one way to do it.

Introduction

When building a console app, it is often necessary to provide various settings. Adding an app.config file to your project allows you to store all your settings in a file that is processed at runtime. When compiled, app.config becomes yourapp.exe.config.

This is great until you need to run a large collection of different settings depending on the circumstance. There are a few ways I have seen to deal with this:

  1. Create a number of config files with the correct keys and before runtime, name the config file you want to use as yourapp.exe.config. Workable, but since you are always renaming files, it may become difficult to determine which settings have been run and which have not.
  2. Create custom tags in the app.config (see simple two-dimensional configuration file), then via another key set which section is the setting you want during the current run. This is the solution I have seen a number of times on the Internet. This is a workable solution, but it can lead to unwieldy app.config files, depending on the number of keys and configurations.

I was not satisfied with both of these solutions. What I wanted was the ability to specify at runtime which config file to use. In this way, I can keep settings for different configurations neatly separated. Here is how I did this.

A Console App

using System;
using System.Configuration;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Net;

namespace yournamepace
{
    class Program
    {
        static void Main(string[] args)
        {
            // get config file from runtime args
            // or if none provided, request config
            // via console
            setConfigFileAtRuntime(args);

            // Rest of your console app
        }
    }

    protected static void setConfigFileAtRuntime(string[] args)
    {
        string runtimeconfigfile;

        if (args.Length == 0)
        {
            Console.WriteLine("Please specify a config file:");
            Console.Write("> "); // prompt
            runtimeconfigfile = Console.ReadLine();
        }
        else
        {
            runtimeconfigfile = args[0];
        }

        // Specify config settings at runtime.
        System.Configuration.Configuration config
    = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        config.AppSettings.File = runtimeconfigfile;
        config.Save(ConfigurationSaveMode.Modified);
        ConfigurationManager.RefreshSection("appSettings");
    }
}

app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <!--<span class="code-comment">
    App settings are specified at runtime.
    Need to specify at minimum this stub file
    --></span>
    <appSettings></appSettings>
</configuration>

Typical Runtime Config File 1

<appSettings>
    <add key="anykey1" value="anyvalue1" />
    <add key="anykey2" value="anyvalue2"/>
</appSettings>

Typical Runtime Config File 2

<appSettings>
    <add key="anykey1" value="anothervalue1" />
    <add key="anykey2" value="anothervalue2"/>
</appSettings>

Now, at runtime, you do the following from the command prompt:

c:\> yourapp.exe myruntimeconfigfile1.config
c:\> yourapp.exe myruntimeconfigfile2.config

Or just double click yourapp.exe, and you will be prompted for a config file.

History

  • 15th June, 2006: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

braditude
Web Developer
United States United States
Software Design Engineer working at Bluetooth Special Interest Group.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
You must Sign In to use this message board.
Spacing  Layout  Per page   
QuestionGreat Tip Pin
Member 1139830617-Apr-15 8:27
memberMember 1139830617-Apr-15 8:27 
Questionnot working. Pin
KARTHIK Bangalore7-Jan-14 19:43
memberKARTHIK Bangalore7-Jan-14 19:43 
GeneralOther Section and SectionGroups Pin
Tobias Manthey23-Jan-09 0:17
memberTobias Manthey23-Jan-09 0:17 
GeneralRe: Other Section and SectionGroups Pin
tharnjaggar8-Nov-10 1:04
membertharnjaggar8-Nov-10 1:04 
GeneralRe: Other Section and SectionGroups Pin
Mr. Noname30-Dec-10 7:28
memberMr. Noname30-Dec-10 7:28 
Questionencrypting ConnectionStrings section in App.Config file? Pin
Rakesh.Gunijan15-Mar-07 3:37
memberRakesh.Gunijan15-Mar-07 3:37 
Jokeworkable option (c#) [modified] Pin
dvhh3-Jul-06 14:30
memberdvhh3-Jul-06 14:30 
GeneralRe: workable option (c#) Pin
Jürgen Müller (jmse)4-Jul-06 2:09
memberJürgen Müller (jmse)4-Jul-06 2:09 
GeneralRe: workable option (c#) Pin
Jürgen Müller (jmse)4-Jul-06 23:00
memberJürgen Müller (jmse)4-Jul-06 23:00 
Questionnot working Pin
Jürgen Müller (jmse)3-Jul-06 1:36
memberJürgen Müller (jmse)3-Jul-06 1:36 
QuestionRe: not working Pin
anand vivek29-Sep-08 3:46
memberanand vivek29-Sep-08 3:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02-2016 | 2.8.180417.1 | Last Updated 15 Jun 2006
Article Copyright 2006 by braditude
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid