使用 ConfigurationManager 从任意位置加载配置 [英] Using ConfigurationManager to load config from an arbitrary location

查看:21
本文介绍了使用 ConfigurationManager 从任意位置加载配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个数据访问组件,该组件将在包含经典 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 页面,这非常有效.但是,当从经典 ASP 页面通过 COM 互操作调用该组件时,该组件并未在 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,我的组件可以回退到那个状态.

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.

欢迎使用任何其他方法!

Any other approaches to this would be welcome!

推荐答案

试试这个:

System.Configuration.ConfigurationFileMap fileMap = new ConfigurationFileMap(strConfigPath); //Path to your config file
System.Configuration.Configuration configuration = System.Configuration.ConfigurationManager.OpenMappedMachineConfiguration(fileMap);

这篇关于使用 ConfigurationManager 从任意位置加载配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆