December 06, 2006

dotNET - Use app.config ApplicationSettings and UserSettings

When using Settings in an Assembly or .exe you can use the Settings Designer to generate a config file using Settings. The Settings Designer provides a wrapper class which allows you to provide defaults and access the config data using Properties.
But what if you're not working inside that Assembly or .exe? this presents a problem.
If your loading the Assembly externally and want to access that Assembly's .config file you'll probably wish to use something in the System.Configuration namespace... unfortunately it's not of much use if you've created the .config file from the Settings Designer in Visual Studio!!
This is because the Designer creates Sections and ApplicationSettings and UserSettings, the System.Configuration namespace does not provide a method to access these (it has a method to access AppSettings which are a different thing.
Below I've written a workaround which locates the app.config and accesses the ApplicationSettings and UserSettings using XML instead of System.Configuration.

_server = GetAssemblySettingOrDefault(Path.Combine("C:\temp\MyAssembly.dll"), "serverLocation", "http://localhost/mydefaultserver/mydefaultserver.asmx");

Return a key/value list of ApplicationSettings and UserSettings for a given .exe's of .dll's .config file.

public static KeyValueConfigurationCollection GetAssemblySettings(string assemblyPath)

Configuration config = ConfigurationManager.OpenExeConfiguration(assemblyPath);

XmlDocument dom = new XmlDocument();

//UserSettings and ApplicationSettings
KeyValueConfigurationCollection returnList = new KeyValueConfigurationCollection();

string[] settingsTypes = { "applicationSettings", "userSettings" };
foreach (string settingType in settingsTypes)
XmlNode node = dom.SelectSingleNode("//configuration//" + settingType);
if (node != null)
if (node.HasChildNodes)
foreach (XmlNode childNode in node.ChildNodes)
if (childNode.HasChildNodes)
foreach (XmlNode settingNode in childNode.ChildNodes)
if (settingNode != null)//the Settings node
if (settingNode.Attributes.Count > 0) //there should be at least one attribute
returnList.Add(settingNode.Attributes[0].Value, settingNode.InnerText);//the setting name and the setting/value

catch { throw; }
return returnList;

// Get the value of the Assembly ApplicationSetting or UserSetting from the key.
// If the key does not have a value then return the default supplied by the caller.

public static string GetAssemblySettingOrDefault(string assemblyPath, string assemblySettingKey, string assemblySettingDefaultValue)
string result = assemblySettingDefaultValue;
KeyValueConfigurationCollection settings = GetAssemblySettings(assemblyPath);
if(settings != null)
KeyValueConfigurationElement key = settings[assemblySettingKey];
if(key != null)
result = key.Value;
return result;


Anonymous said... for me.

Sameer said...

You are a Champ. Thanks

ASP NET Web Services said...

Great work dude.... keep it up...

DotNetURL said...


Nice blog..publish you new content url at and get reader and back link form us for free

web design company,web designer,web design India,website design,web design said...

Thanks u r information

Suparna said...

Thank you for sharing such an informative article with us.
Web Design  |   Web Designer

Anonymous said...

come to visit my .NET space :

Windows Phone 7 Application Development said...

Thanks for the nice information.This will help a lot of users.

mani said...

very usefull notes and we need more information on this topic please post updated data . thanks for your post.
Dot net online tutorials