ConfigurationManager.RefreshSection(“AppSettings”) 不起作用

发布于 2024-09-06 13:55:30 字数 733 浏览 1 评论 0原文

我试图让(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 并相应地使用它们。

这确实应该简单得多。

谢谢瑞安

I am trying to get a (ClickOnce deployed) application to read the non-default config file, depending on an environment variable set on the

_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");

This doesn't work! i.e. if the env varibale ENV is set to dvlp, the code runs, but values in the file app.dvlp.config are not accessible.

Can anyone see why this doesn't work? Or suggest a way I can have separate files, app.xxx.config for each environment / machine and have them used accordingly.

This really ought to be much more simple.

Thanks

Ryan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

对你再特殊 2024-09-13 13:55:30

这里是来自 Microsoft ClickOnce 产品负责人的一篇博客文章,介绍了处理不同构建配置的 app.config 文件的方法。希望有帮助。

Here is a blog article from the ClickOnce product lead at Microsoft with a way to handle app.config files for different build configurations. Hope it helps.

错爱 2024-09-13 13:55:30

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

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

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

I'm pretty sure you can't access stuff outside of your sand boxed area with a ClickOnce install.

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

That said if I'm wrong I'd love to know the answer too :)

知你几分 2024-09-13 13:55:30

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

are you passing correct parameter I noticed you have capital AppSettings not appSettings

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文