【发布时间】:2014-05-29 17:55:21
【问题描述】:
如何通过 ConfigurationManager 找到配置文件的位置?
我在代码中有 ConfigurationManager 类,我正在调试它。我想知道它指向哪个配置文件(web.config 或 app.config 等)。
ConfigurationManager 上是否有任何属性或方法可以帮助解决这个问题?
【问题讨论】:
标签: asp.net .net configurationmanager
如何通过 ConfigurationManager 找到配置文件的位置?
我在代码中有 ConfigurationManager 类,我正在调试它。我想知道它指向哪个配置文件(web.config 或 app.config 等)。
ConfigurationManager 上是否有任何属性或方法可以帮助解决这个问题?
【问题讨论】:
标签: asp.net .net configurationmanager
配置文件本身由Configuration对象表示。要获取此对象,请运行以下命令:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
然后可以通过config.FilePath查看文件路径。
更新。正如Schadensbegrenzer 所指出的,对于 Web 应用程序,您将需要另一个代码来加载配置文件:
Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
【讨论】: