[英]How to programmatically retrieve the configSource Location from config file
Does anyone know how i can get the configSource value using standard API? 有谁知道如何使用标准API获取configSource值?
<appSettings configSource="AppSettings.config" />
Or do i need to parse the web.config in XML to get the value? 或者我需要解析XML中的web.config以获取值?
You need to load the AppSettingsSection , then access its ElementInformation.Source property. 您需要加载AppSettingsSection ,然后访问其ElementInformation.Source属性。
The link above contains information about how to access this section. 上面的链接包含有关如何访问此部分的信息。
Try 尝试
ConfigurationManager.AppSettings["configSource"]
you need to add : using System.Configuration;
你需要添加:
using System.Configuration;
namespace in your code 代码中的命名空间
Need to use the config manager as @competent_tech mentioned. 需要使用配置管理器作为@competent_tech提到。
//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. 无法使用@dbugger和@comptent_tech的建议正确加载AppSettings部分的API。
Unable to cast object of type 'System.Configuration.DefaultSection' to type
'System.Configuration.AppSettingsSection'.
'System.Configuration.AppSettingsSection'。
Eventually went the XML route in just as many lines of code: 最终在尽可能多的代码行中使用了XML路由:
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. 感谢所有指针。
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.