【问题标题】:Find the name of the current config file查找当前配置文件的名称
【发布时间】:2011-02-06 17:19:36
【问题描述】:

有没有办法从类库中找到当前应用程序的配置文件的名称(和路径)?

例如在 Web 应用程序中,这将是 web.config,在 Windows 应用程序或服务中,这将是 myapp.exe.config

【问题讨论】:

    标签: .net configuration web-config app-config


    【解决方案1】:

    试试这个:

    AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
    

    如果这不起作用,请添加对 System.Web 和 System.Configuration 的引用,然后试试这个:

    if(HttpRuntime.AppDomainAppVirtualPath  == null) 
        return ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath;
    else
        return VirtualPathUtility.ToAbsolute("~/Web.config");
    

    【讨论】:

    • 谢谢。在第一次测试之后,第一种方法似乎适用于 Windows 服务和 Web 应用程序。
    • 我从不立即接受答案(但我会接受),例如能够验证解决方案并让其他人有机会回答。
    【解决方案2】:

    使用Configuration.FilePath:

    获取此配置对象表示的配置文件的物理路径。

    【讨论】:

    • 是的,但是你需要先有一个“配置”元素......不幸的是没有“Configuration.CurrentElement”或类似的东西......
    • 我如何获得配置实例?
    猜你喜欢
    • 2011-08-18
    • 2013-08-02
    • 2021-02-07
    • 1970-01-01
    • 2012-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-03
    相关资源
    最近更新 更多