Does anyone know how i can get the configSource value using standard API?
<appSettings configSource="AppSettings.config" />
Or do i need to parse the web.config in XML to get the value?
You need to load the AppSettingsSection, then access its ElementInformation.Source property.
The link above contains information about how to access this section.
Need to use the config manager as @competent_tech mentioned.
//open the config file..
Configuration config= ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
//read the ConfigSource
string configSourceFile = config.AppSettings.SectionInformation.ConfigSource;
Couldn't get the API to correctly load the AppSettings section correctly using the suggestions from @dbugger and @competent_tech.
Unable to cast object of type 'System.Configuration.DefaultSection' to type'System.Configuration.AppSettingsSection'.
Eventually went the XML route in just as many lines of code:
XDocument xdoc = XDocument.Load(Path.Combine(Server.MapPath("~"), "web.config"));
var query = from e in xdoc.Descendants("appSettings")
select e;
return query.First().Attribute("configSource").Value;
Thanks to all for the pointers.
If, like me you want to read ALL the configSource's in the app.config file....here is a loop for it.
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
foreach (ConfigurationSection section in config.Sections)
{
if (!string.IsNullOrEmpty(section.SectionInformation.ConfigSource))
{
Console.WriteLine("ConfigSource={0}", section.SectionInformation.ConfigSource)
// this is all your ConfigSource nodes.
}
}
You can use:
<appSettings>
<add key="configSource" value="AppSettings.config"/>
<add key="anotherValueKey" value="anotherValue"/>
<!-- You can put more ... -->
</appSettings>
And retrieve the value:
string value = ConfigurationManager.AppSettings["configSource"];
string anotherValue = ConfigurationManager.AppSettings["anotherValueKey"];
don't forget:
using System.Configuration;