简体   繁体   English

如何以编程方式从配置文件中检索configSource位置

[英]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.

相关问题 在configSource中使用外部.config文件会产生错误 - Using an external .config file in configSource produces error 如何以编程方式获取user.config文件的位置? - How do I get the location of the user.config file in programmatically? 如何以编程方式从web.config中检索smtp服务器详细信息 - How to programmatically retrieve smtp server details from web.config 将更改保存到自定义部分的configSource属性中指定的外部配置文件中 - Saving changes to an external config file specified in configSource attribute of custom section 如何为ConfigSections和connectionStrings使用通用的ConfigSource文件? - How to have a common ConfigSource file for ConfigSections and connectionStrings? 单元测试时未应用Web.config Configsource和文件属性 - Web.config Configsource and file attributes not applied when unit testing 从 config 加载一些,从 configSource 加载一些 - Loading some from config and some other from configSource 从使用configSource文件的Web.Config中读取值 - Reading value from Web.Config that uses configSource files 如何以编程方式加载配置文件 - How to Load Config File Programmatically 如何从 web.config 文件中的 xml 参数中检索值? - How to retrieve value from xml parameter in web.config file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM