How to programmatically retrieve a configuration location from a configuration file

Does anyone know how I can get the configSource value using the standard API?

<appSettings configSource="AppSettings.config" /> 

Or do I need to parse web.config in XML to get the value?

+3
source share
5 answers

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

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

+3
source

Try

  ConfigurationManager.AppSettings["configSource"] 

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

+1
source

You must use the configuration manager as indicated by @competent_tech.

 //open the config file.. Configuration config= ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); //read the ConfigSource string configSourceFile = config.AppSettings.SectionInformation.ConfigSource; 
+1
source

Failed to get the API to load the AppSettings section correctly using offers from @dbugger and @competent_tech.

 Unable to cast object of type 'System.Configuration.DefaultSection' to type 

'System.Configuration.AppSettingsSection'.

In the end, the XML route went through 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 everyone for the pointers.

0
source

You can use:

 <appSettings> <add key="configSource" value="AppSettings.config"/> <add key="anotherValueKey" value="anotherValue"/> <!-- You can put more ... --> </appSettings> 

And get the value:

 string value = ConfigurationManager.AppSettings["configSource"]; string anotherValue = ConfigurationManager.AppSettings["anotherValueKey"]; 

Do not forget:

 using System.Configuration; 
-one
source

Source: https://habr.com/ru/post/1496865/


All Articles