asp.net - 使用 ConfigurationManager 从任意位置加载配置

标签 asp.net configuration asp-classic

我正在开发一个数据访问组件,该组件将用于包含经典 ASP 和 ASP.NET 页面混合的网站,并且需要一种管理其配置设置的好方法。

我想使用自定义 ConfigurationSection,这对于 ASP.NET 页面非常有效。但是,当从经典 ASP 页面通过 COM 互操作调用该组件时,该组件不会在 ASP.NET 请求的上下文中运行,因此不了解 web.config。

有没有办法告诉 ConfigurationManager 只从任意路径加载配置(例如 ..\web.config 如果我的程序集在 /bin 文件夹)?如果有,那么我认为如果默认 ConfigurationManager.GetSection 为我的自定义部分返回 null,我的组件可以回退到那个。

欢迎使用任何其他方法!

最佳答案

试试这个:

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

关于asp.net - 使用 ConfigurationManager 从任意位置加载配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9236936/

相关文章:

c# - 如果 C# 中的文件夹中存在文件名更改

asp.net - linq 中的 switch 语句

c# - 使用 CSS 样式将从数据库中检索到的图像显示为列表

iis-7 - VBScript 日期格式与区域设置不匹配

.net - 使用 Web.config 将 HTML 解析为 ASP,但当页面中存在 JavaScript 时出现错误

javascript - 我的留言簿有问题

python - config.py 模块的配置文件中的函数 eval

c# - 如何将 appsettings.json 文件添加到我的 Azure Function 3.0 配置中?

configuration - Akka .conf 调度程序配置是否有效?

asp.net - 关于使用 ASP.net WebForms 或 MVC 的建议