Blog » Recursing Through AppConfig File

Recursing Through AppConfig File

While doing some updates to the EmmCLA program (more to come on this soon), I needed to have it loop through all the AppSettings in in App.Config file (the same should hold true for Web.Config for web apps) to find all occurrences of appsettings that were similar to a certain string.

A few Google searches later led me to the solution of using System.Configuration.ConfigurationSettings.AppSettings.AllKeys which returns a string array of all the keys in the AppSettings portion of App.Config:

// Loop through all AppSetting keys to find those 
// matching DBConn-xxxx
string[] AppSettings = ConfigurationSettings.AppSettings.AllKeys;foreach (string s in AppSettings){if (s.ToLower().Contains("dbconn-")){
// Do Something Here
}}

I’ve posted this in case anyone else finds a need to do something similar. This saved me a great deal of work and hopefully someone else will similarly benefit.

If you find this of some benefit, please leave us a comment below and let us know how you are using it.

Michael Gibbs
Stalk Me...
Latest posts by Michael Gibbs (see all)