【问题标题】:How to find the Config File location via ConfigurationManager?如何通过 ConfigurationManager 找到配置文件的位置?
【发布时间】:2014-05-29 17:55:21
【问题描述】:

如何通过 ConfigurationManager 找到配置文件的位置?

我在代码中有 ConfigurationManager 类,我正在调试它。我想知道它指向哪个配置文件(web.config 或 app.config 等)。

ConfigurationManager 上是否有任何属性或方法可以帮助解决这个问题?

【问题讨论】:

    标签: asp.net .net configurationmanager


    【解决方案1】:

    配置文件本身由Configuration对象表示。要获取此对象,请运行以下命令:

    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    

    然后可以通过config.FilePath查看文件路径。

    更新。正如Schadensbegrenzer 所指出的,对于 Web 应用程序,您将需要另一个代码来加载配置文件:

    Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
    

    【讨论】:

    • 您的解决方案不适用于 Web 应用程序。在 Web 应用程序中,您必须使用 System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~")
    猜你喜欢
    • 1970-01-01
    • 2017-08-21
    • 2011-02-17
    • 2010-09-05
    • 2011-12-10
    • 2011-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多