Reputation: 7040
To read the AppSettings section from your current application's app.config is easy - you can just use ConfigurationManager.AppSettings, which returns a series of name/value pairs you can read.
But what if you want to access the entries from another application's config file?
I see ConfigurationManager has other methods like OpenExeConfiguration, but when I follow these methods all the way down, I don't see any way to iterate over an AppSetting (or any section for that matter) as a series of name/value pairs, such as is available with ConfigurationManager.AppSettings.
Is it possible to read a separate exe's config files and easily iterate (not just access by individual keys)?
Upvotes: 2
Views: 1878
Reputation: 407
There's probably a cleaner way to do this, but this works (showing both grabbing a specific value and building a dictionary so you can iterate).
private string collection;
Dictionary<string, string> settings = new Dictionary<string, string>();
// using System.Configuration;
private void LoadOthersConfig(string exepath)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(exepath);
if (config.Sections["appSettings"] != null)
{
AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
collection = appSettings.Settings["collection"].Value;
foreach (string k in appSettings.Settings.AllKeys)
{
settings.Add(k, appSettings.Settings[k].Value);
}
}
}
Upvotes: 1
Reputation: 564333
Application configuration files are XML files, so you could parse them using LINQ to XML. This would allow you to easily read an application configuration file and get any information you chose from it.
Upvotes: 0