5

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?

6 Answers 6

3

You need to load the AppSettingsSection, then access its ElementInformation.Source property.

The link above contains information about how to access this section.

Sign up to request clarification or add additional context in comments.

Comments

1

Try

  ConfigurationManager.AppSettings["configSource"]

you need to add : using System.Configuration; namespace in your code

Comments

1

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;

2 Comments

Yeah was onto that. Seems like this code only works in EXE context. Any ideas how to get it to work in ASP.NET context?
Sure...Configuration config = ConfigurationManager.OpenExeConfiguration(Path.Combine(Server.MapPath("~"), "web.config"));
0

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.

Comments

0

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.
   }
}

Comments

-1

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;

3 Comments

Then how do i access the keys in AppSettings.config? Will ASP.NET load the external config file?
I don't understand you. Do you want to load an AppSettings.config file? You can put some values or global parameters for your application in AppSettings section, then you retrieve that values. i edited my previous post.
Sorry i may not have been clear in my OP. .NET configuration allows you to reference and store config data in external files. All my appSettings keys are in external files.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.