.NET application output app.config settings at runtime nicely formatted

Tue, Apr 25, 2017 2-minute read

I’m a big fan of .NET console applications. They’re a really quick way of getting something functional up and running fast without wasting loads of effort on user interfaces. (Hey, I’m a developer, OK… not a designer. Function over form and all that.)

I also make heavy use of the built-in app.config / settings functionality so that I can control functionality at runtime. One thing I wanted to do recently was to output the configured settings to the console window so that I could eyeball all was well before going ahead.

So for reference, here’s a bit of code that will do exactly that - get all settings that have been configured in a .NET settings and then tabulate/format it nicely so it’s readable. Not rocket science, but it may help someone:

public void OutputProperties()
            var propertyNameMaxLength = (from SettingsProperty setting in Properties.Settings.Default.Properties select setting.Name.Length).Concat(new[] { 0 }).Max();
            foreach (SettingsProperty setting in Properties.Settings.Default.Properties.OfType<settingsproperty>().OrderBy(x => x.Name))
                LogInfoWithHighlight(string.Format("{0}\t{1}", setting.Name.PadRight(propertyNameMaxLength), Properties.Settings.Default[setting.Name]));


The first line just gets the length of the longest property so that it can then pad all shorter ones, and then insert a tab and output the values. The rest just orders the list by the setting name (alphabetised) and pumps them out to the screen. Note that we have to index back in to the Settings.Default collection using the setting name - if you use the .DefaultValue property then you will get the… default value (not the value used at runtime.)

This produces something like this:


FYI, the LogInfoWithHighlight is a just a little helper function in a wrapper class that I reuse for putting stuff out to the console. It looks like this, but you could do anything:

public void LogInfoWithHighlight(string msg)
            Console.ForegroundColor = ConsoleColor.Green;

Hope this helps.