【问题标题】:Get the file path of current application's config file获取当前应用的配置文件的文件路径
【发布时间】:2011-02-26 18:35:22
【问题描述】:

我问this question 的原因是我想为远程实例化创建一个帮助类,并且想将适当的 app.exe.config(或 web.config)文件路径传递给RemotingConfiguration.Configure 方法,具体取决于在来电者身上。

有没有一种方法可以在不检查应用程序是 Web 还是 WinForms 的情况下获取 Win 和 Web 应用程序配置文件的名称?

【问题讨论】:

    标签: c# .net web-config app-config


    【解决方案1】:

    查找正在执行的应用程序的位置

    System.Reflection.Assembly.GetExecutingAssembly().Location;
    

    不知道网络案例。

    【讨论】:

    • 我猜你的意思是System.Reflection.Assembly.GetExecutingAssembly().Location + ".config",但实际上我需要一个适用于这两个应用程序的解决方案。
    【解决方案2】:

    我用过

    string folder = System.Web.HttpContext.Current != null ?
        System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_data") :
        System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
    

    没有问题,但也许有一些我不知道的极端情况......

    【讨论】:

    • “App_Data”的大写字母“D”。
    【解决方案3】:

    您可以将ConfigurationFile property 上的SetupInformation 用于AppDomain.CurrentDomain

    这将获得 web.config 或 app.config (yourprogram.exe.config) 位置。

    AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
    

    【讨论】:

    • 老实说,这个答案让我觉得不言自明;成员名称非常具有描述性。
    • 请注意,.NET Standard 2.0 不支持此功能
    猜你喜欢
    • 1970-01-01
    • 2021-11-02
    • 1970-01-01
    • 1970-01-01
    • 2022-07-22
    • 2011-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多