【问题标题】:ConfigurationManager.RefreshSection("AppSettings") doesn't workConfigurationManager.RefreshSection("AppSettings") 不起作用
【发布时间】:2025-11-21 08:50:01
【问题描述】:

我正在尝试让(ClickOnce 部署的)应用程序读取非默认配置文件,具体取决于在

上设置的环境变量
_envName = System.Environment.GetEnvironmentVariable("ENV");
if (_envName == null)
    throw new Exception ("The ENV environemnt variable must be set");

string envFileName = "app." + _envName.ToLower() + ".config";

System.Configuration.Configuration config = 
     ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.File = envFileName;

config.Save(ConfigurationSaveMode.Full);
ConfigurationManager.RefreshSection("AppSettings");

这行不通!即,如果环境变量 ENV 设置为 dvlp,则代码运行,但文件 app.dvlp.config 中的值不可访问。

谁能明白为什么这不起作用?或者建议一种方法,我可以为每个环境/机器设置单独的文件 app.xxx.config 并相应地使用它们。

这真的应该简单得多。

谢谢

瑞恩

【问题讨论】:

    标签: c# configuration clickonce app-config environment


    【解决方案1】:

    Here 是 Microsoft ClickOnce 产品负责人的一篇博客文章,其中介绍了针对不同构建配置处理 app.config 文件的方法。希望对您有所帮助。

    【讨论】:

      【解决方案2】:

      我很确定您无法通过 ClickOnce 安装访问沙盒区域之外的内容。

      http://msdn.microsoft.com/en-us/library/d8saf4wy.aspx

      也就是说,如果我错了,我也很想知道答案:)

      【讨论】:

        【解决方案3】:

        你是否传递了正确的参数我注意到你有大写的 AppSettings 而不是 appSettings

        【讨论】:

          最近更新 更多