我正在开发一个数据访问组件,该组件将用于包含经典 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/