2

Let's say I have a command line application that needs to reference some form of a user configuration file. Furthermore, the values contained within this file are to only be updated by the user manually -- there won't be any ways of updating the configuration file within the application, nor will the application seek out any inputs once started. If a configuration listing is missing from the configuration file, a default value should be used instead.

I understand that Visual Studio / .NET Framework offer tools for creating such constructs (i.e. Settings and App.configs), but I'm not sure I'm using them correctly -- or if I should be using them at all.

I created a Settings file and threw in a couple of default settings (e.g. SomeBooleanFlag is a bool with a default value of 'False'). This addition of course was reflected within my App.config. However, here is where the dilemma lies: how should I read from the App.config?

Currently, I've created a class to abstract away the configuration manager / settings stuff:

class AppSettings
{
    public static bool SomeBooleanFlag
    {
        get
        {
            try
            {
                string rawValue = ConfigurationManager.AppSettings["SomeBooleanFlag"];

                bool userSetSomeBooleanFlag;
                bool userValueParsed = bool.TryParse(rawValue, out userSetSomeBooleanFlag);

                return (userValueParsed) ? userSetSomeBooleanFlag : Settings.Default.SomeBooleanFlag;
            }
            catch
            {             
                return Settings.Default.SomeBooleanFlag;
            }                
        }
    }
}

Which then gives me the ability to write:

if (AppSettings.SomeBooleanFlag) 
{ 
    /* Do Something... */ 
}

However, this does not seem like a clean way to approach the problem I stated above. Any help would be greatly appreciated!

kylemart
  • 1,156
  • 1
  • 13
  • 25
  • I use a generic helper function instead of a class, but otherwise your code seems good to me – BradleyDotNET Sep 13 '16 at 17:25
  • Why isn't this a "clean way to approach the problem"? What's wrong with it? You're basically doing what a lot of good app designers do, by abstracting out the settings repository. All your business code needs to do is call your AppSettings to get the values, and you're free to change the source of the underlying settings later, like to the registry or a database, without affecting the rest of the application. But without knowing your *specific* concern, I can't know what you're really asking. – rory.ap Sep 13 '16 at 17:28
  • You can use the [appSettings file attribute](https://msdn.microsoft.com/en-us/library/system.configuration.appsettingssection.file(v=vs.110).aspx) to specify a [separate config file to override the default settings](http://stackoverflow.com/questions/27691636/write-appsettings-in-external-file). – Jasen Sep 13 '16 at 17:54

1 Answers1

1

Instead of coding your own Application Settings wrapper, you can reuse the functionality built into Visual Studio to generate this wrapper for you. See How to: Add or Remove Application Settings and How To: Read Settings at Run Time With C#. In addition to specifying the type of the setting you can also indicate the scope (User or Application).

Following the steps in the aforementioned articles, it will add the settings to your App.config file, below is an example:

<configuration>
...
    <applicationSettings>
        <ConsoleApplication1.Properties.Settings>
            <setting name="TestKey" serializeAs="String">
                <value>TestValue</value>
            </setting>
        </ConsoleApplication1.Properties.Settings>
    </applicationSettings>
...
</configuration>

And you can access these settings as follows:

string s = Properties.Settings.Default.TestKey;
sly
  • 300
  • 1
  • 5