使用ConfigurationManager从任意位置加载配置 [英] Using ConfigurationManager to load config from an arbitrary location
问题描述
我正在开发一个数据访问组件,将在包含经典ASP和ASP.NET页面的网站中使用,需要一个好的方法来管理其配置设置。
I'm developing a data access component that will be used in a website that contains a mix of classic ASP and ASP.NET pages, and need a good way to manage its configuration settings.
我想使用自定义的 ConfigurationSection
,对于ASP.NET页面,这是非常好的。但是当组件通过COM interop从一个经典的ASP页面调用时,该组件不在ASP.NET请求的上下文中运行,因此不知道web.config。
I'd like to use a custom ConfigurationSection
, and for the ASP.NET pages this works great. But when the component is called via COM interop from a classic ASP page, the component isn't running in the context of an ASP.NET request and therefore has no knowledge of web.config.
有没有办法告诉 ConfigurationManager
只是从任意路径加载配置(例如 .. \web.config
如果我的程序集在 / bin
文件夹中)?如果有,我想我的组件可以回到,如果默认 ConfigurationManager.GetSection
返回 null
for
Is there a way to tell the ConfigurationManager
to just load the configuration from an arbitrary path (e.g. ..\web.config
if my assembly is in the /bin
folder)? If there is then I'm thinking my component can fall back to that if the default ConfigurationManager.GetSection
returns null
for my custom section.
推荐答案
尝试以下操作:
System.Configuration.ConfigurationFileMap fileMap = new ConfigurationFileMap(strConfigPath); //Path to your config file
System.Configuration.Configuration configuration = System.Configuration.ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
这篇关于使用ConfigurationManager从任意位置加载配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!