Thursday, April 26, 2007

Retrieving application settings from another assembly

I was writing a windows service that referenced a DLL I had previously written when I came across a problem reading the ApplicationSettings of the parent EXE. Well after starting down the path of writing my own class to accomplish this I came across this post from "Niall Fallon".
Well he/she had done exactly what I was working on so I had to copy their post here so I would have it later.
Thank you Niall

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();
dom.Load(config.FilePath);

//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)
{
try
{
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;
}

1 comment:

Anonymous said...

Interesting to know.